Team Ai
Apppublic

3emibrahim/debugging-testing2

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
utils.py47 linesDownload Raw Back to backend
1import io
2import os
3import cloudinary
4import cloudinary.uploader
5from tika import parser
6from huggingface_hub import InferenceClient
7
8# Cloudinary Config
9cloudinary.config(
10    cloud_name="dsrjsulxu",
11    api_key="494988785133644",
12    api_secret="9CroDeskvcA5ZoNiNoZddHguny0"
13)
14
15# Hugging Face Client
16client = InferenceClient(api_key=os.getenv("api_key"))
17
18
19def extract_text(file_bytes: bytes, filename: str) -> str:
20    ext = filename.split('.')[-1].lower()
21    if ext not in {"pdf", "pptx", "xlsx"}:
22        return None
23    parsed = parser.from_buffer(io.BytesIO(file_bytes))
24    return parsed.get("content", "").strip()
25
26
27def upload_image(file_bytes: bytes) -> str:
28    try:
29        result = cloudinary.uploader.upload(io.BytesIO(file_bytes))
30        return result.get("secure_url")
31    except Exception as e:
32        print(f"Cloudinary upload error: {e}")
33        return None
34
35
36def generate_response(content: list) -> str:
37    try:
38        response = client.chat.completions.create(
39            model="google/gemma-3-27b-it",
40            messages=[{"role": "user", "content": content}],
41            max_tokens=500
42        )
43        return response.choices[0].message["content"]
44    except Exception as e:
45        print(f"Hugging Face API error: {e}")
46        return "Failed to generate response from the model."
47