Spaces:
Building
Building
gojossatoru
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse, PlainTextResponse
|
3 |
+
from pyngrok import ngrok
|
4 |
+
import base64
|
5 |
+
import requests
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
API_URL = "https://api.hyperbolic.xyz/v1/chat/completions"
|
13 |
+
API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJnb2pvNzk0ODRAZ21haWwuY29tIn0.J4bPeGA4-6tHXYNz1FHi9WSJ0gY_7MW2E9m28aGBh6g"
|
14 |
+
|
15 |
+
@app.post("/upload-image/")
|
16 |
+
async def upload_image(file: UploadFile = File(...)):
|
17 |
+
try:
|
18 |
+
# Read and convert the image to base64
|
19 |
+
img = Image.open(BytesIO(await file.read()))
|
20 |
+
buffered = BytesIO()
|
21 |
+
img.save(buffered, format="PNG") # Save in PNG format
|
22 |
+
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
23 |
+
|
24 |
+
# Send request to the API
|
25 |
+
headers = {
|
26 |
+
"Content-Type": "application/json",
|
27 |
+
"Authorization": f"Bearer {API_KEY}",
|
28 |
+
}
|
29 |
+
|
30 |
+
payload = {
|
31 |
+
"messages": [
|
32 |
+
{
|
33 |
+
"role": "user",
|
34 |
+
"content": [
|
35 |
+
{"type": "text", "text": "Toplam ödeme tutarı ne kadardır?"},
|
36 |
+
{
|
37 |
+
"type": "image_url",
|
38 |
+
"image_url": {"url": f"data:image/png;base64,{encoded_string}"},
|
39 |
+
},
|
40 |
+
],
|
41 |
+
}
|
42 |
+
],
|
43 |
+
"model": "Qwen/Qwen2-VL-72B-Instruct",
|
44 |
+
"max_tokens": 2048,
|
45 |
+
"temperature": 0.7,
|
46 |
+
"top_p": 0.9,
|
47 |
+
}
|
48 |
+
|
49 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
50 |
+
|
51 |
+
if response.status_code == 200:
|
52 |
+
content = response.json()["choices"][0]["message"]["content"]
|
53 |
+
return PlainTextResponse(content) # Return as plain text
|
54 |
+
else:
|
55 |
+
return JSONResponse(content={"error": response.json()}, status_code=response.status_code)
|
56 |
+
except Exception as e:
|
57 |
+
raise HTTPException(status_code=500, detail=str(e))
|
58 |
+
|
59 |
+
def process_image(image):
|
60 |
+
# Convert the Gradio image input to a format suitable for FastAPI
|
61 |
+
buffered = BytesIO()
|
62 |
+
image.save(buffered, format="PNG")
|
63 |
+
buffered.seek(0)
|
64 |
+
|
65 |
+
# Create an UploadFile-like object
|
66 |
+
file_like = UploadFile(
|
67 |
+
filename="image.png",
|
68 |
+
file=buffered,
|
69 |
+
content_type="image/png"
|
70 |
+
)
|
71 |
+
|
72 |
+
return upload_image(file_like)
|
73 |
+
|
74 |
+
# Gradio interface
|
75 |
+
gr_interface = gr.Interface(
|
76 |
+
fn=process_image,
|
77 |
+
inputs=gr.inputs.Image(type="pil"),
|
78 |
+
outputs="text",
|
79 |
+
title="Image Upload",
|
80 |
+
description="Upload an image to get information about the payment amount."
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
public_url = ngrok.connect(8000)
|
85 |
+
print(f"Ngrok URL: {public_url}")
|
86 |
+
|
87 |
+
# Launch Gradio app
|
88 |
+
gr_interface.launch(share=True)
|
89 |
+
|
90 |
+
# Start FastAPI
|
91 |
+
import uvicorn
|
92 |
+
uvicorn.run(app, port=8000)
|