fix
Browse files- main.py +34 -37
- 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 |
-
#
|
| 81 |
image_bytes = await file.read()
|
| 82 |
encoded_image = base64.b64encode(image_bytes).decode('utf-8')
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 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
|