lruoppolo commited on
Commit
250de0a
·
1 Parent(s): 0e8c2ac
Files changed (2) hide show
  1. main.py +34 -37
  2. requirements.txt +2 -1
main.py CHANGED
@@ -26,6 +26,11 @@ except Exception as e:
26
  app = FastAPI()
27
  security = HTTPBearer()
28
 
 
 
 
 
 
29
  # --- UTILITY ---
30
  async def send_to_telegram(message: str):
31
  url = f"https://api.telegram.org/bot{config.TELEGRAM_BOT_TOKEN}/sendMessage"
@@ -77,46 +82,38 @@ async def root():
77
  async def extract_image(uid: str = Depends(verify_firebase_token), file: UploadFile = File(...)):
78
  await send_to_telegram(f"Nuova richiesta VLM da {uid}")
79
 
80
- # 1. Leggi i bytes e convertili in Base64
81
  image_bytes = await file.read()
82
  encoded_image = base64.b64encode(image_bytes).decode('utf-8')
83
 
84
- # 2. Prepara il payload esattamente come nella tua curl
85
- payload = {
86
- "model": "ggml-org/Qwen2.5-VL-7B-Instruct-GGUF",
87
- "messages": [
88
- {
89
- "role": "user",
90
- "content": [
91
- {
92
- "type": "image_url",
93
- "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}
94
- },
95
- {
96
- "type": "text",
97
- "text": "Describe this image in one sentence."
98
- }
99
- ]
100
- }
101
- ],
102
- "stream": True, # Ho impostato True come nella tua curl
103
- "max_tokens": 100
104
- }
105
-
106
- async with httpx.AsyncClient(timeout=120.0) as client:
107
- try:
108
- # 3. Invio come JSON (corrisponde a -H "Content-Type: application/json")
109
- response = await client.post(
110
- config.HF_ENDPOINT_URL,
111
- headers={"Authorization": f"Bearer {config.HF_TOKEN}"},
112
- json=payload # 'json' imposta automaticamente Content-Type: application/json
113
- )
114
- response.raise_for_status()
115
- return response.json()
116
-
117
- except Exception as e:
118
- await send_to_telegram(f"🚨 Errore VLM: {str(e)}")
119
- raise HTTPException(status_code=502, detail=str(e))
120
 
121
  @app.post("/analyze-document")
122
  async def analyze_document(request: Request, uid: str = Depends(verify_firebase_token)):
 
26
  app = FastAPI()
27
  security = HTTPBearer()
28
 
29
+ client = OpenAI(
30
+ api_key=config.HF_TOKEN,
31
+ base_url="https://s5jfvadty4tkdlkr.eu-west-1.aws.endpoints.huggingface.cloud/v1"
32
+ )
33
+
34
  # --- UTILITY ---
35
  async def send_to_telegram(message: str):
36
  url = f"https://api.telegram.org/bot{config.TELEGRAM_BOT_TOKEN}/sendMessage"
 
82
  async def extract_image(uid: str = Depends(verify_firebase_token), file: UploadFile = File(...)):
83
  await send_to_telegram(f"Nuova richiesta VLM da {uid}")
84
 
85
+ # Leggi l'immagine e convertila in base64
86
  image_bytes = await file.read()
87
  encoded_image = base64.b64encode(image_bytes).decode('utf-8')
88
 
89
+ try:
90
+ # La chiamata all'API tramite il client OpenAI
91
+ response = client.chat.completions.create(
92
+ model="ggml-org/Qwen2.5-VL-7B-Instruct-GGUF",
93
+ messages=[
94
+ {
95
+ "role": "user",
96
+ "content": [
97
+ {
98
+ "type": "image_url",
99
+ "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}
100
+ },
101
+ {
102
+ "type": "text",
103
+ "text": "Describe this image in one sentence."
104
+ }
105
+ ]
106
+ }
107
+ ],
108
+ max_tokens=100
109
+ )
110
+
111
+ # Ritorna il risultato
112
+ return response.choices[0].message.content
113
+
114
+ except Exception as e:
115
+ await send_to_telegram(f"🚨 Errore VLM: {str(e)}")
116
+ raise HTTPException(status_code=502, detail=str(e))
 
 
 
 
 
 
 
 
117
 
118
  @app.post("/analyze-document")
119
  async def analyze_document(request: Request, uid: str = Depends(verify_firebase_token)):
requirements.txt CHANGED
@@ -6,4 +6,5 @@ httpx
6
  python-magic
7
  # Aggiunto per il parsing PDF lato backend se necessario in futuro
8
  pymupdf
9
- uvloop
 
 
6
  python-magic
7
  # Aggiunto per il parsing PDF lato backend se necessario in futuro
8
  pymupdf
9
+ uvloop
10
+ openai