Spaces:
Building
Building
gojossatoru
commited on
Commit
•
d46df35
1
Parent(s):
38d9987
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,29 @@
|
|
1 |
-
|
2 |
-
from fastapi.responses import JSONResponse, PlainTextResponse
|
3 |
-
from pyngrok import ngrok
|
4 |
-
import base64
|
5 |
import requests
|
6 |
-
|
7 |
from PIL import Image
|
8 |
-
import
|
|
|
|
|
|
|
9 |
|
10 |
-
app = FastAPI()
|
11 |
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
try:
|
18 |
-
|
19 |
-
|
20 |
-
buffered =
|
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}",
|
@@ -47,47 +49,22 @@ async def upload_image(file: UploadFile = File(...)):
|
|
47 |
}
|
48 |
|
49 |
response = requests.post(API_URL, headers=headers, json=payload)
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
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 |
-
|
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 |
-
|
75 |
-
|
76 |
-
|
77 |
-
inputs=gr.Image(type="pil"), # Updated to use gr.Image
|
78 |
outputs="text",
|
79 |
-
title="
|
80 |
-
description="
|
81 |
)
|
82 |
|
83 |
-
|
84 |
-
ngrok.set_auth_token("2m46ThDvL9VaQVxeVKs47yt9VRb_66BX7RzbfnQwWUj1cQeoV")
|
85 |
-
public_url = ngrok.connect(7860)
|
86 |
-
print(f"Ngrok URL: {public_url}")
|
87 |
-
|
88 |
-
# Launch Gradio app
|
89 |
-
gr_interface.launch(share=True)
|
90 |
-
|
91 |
-
# Start FastAPI
|
92 |
-
import uvicorn
|
93 |
-
uvicorn.run(app, port=7860)
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
|
|
2 |
import requests
|
3 |
+
import io
|
4 |
from PIL import Image
|
5 |
+
import base64
|
6 |
+
|
7 |
+
API_KEY = "YOUR_API_KEY" # API anahtarınızı buraya girin
|
8 |
+
API_URL = "YOUR_API_URL" # API URL'nizi buraya girin
|
9 |
|
|
|
10 |
|
11 |
+
def analyze_image(image_file):
|
12 |
+
"""
|
13 |
+
Görüntüyü analiz eder ve API'den yanıt alır.
|
14 |
|
15 |
+
Args:
|
16 |
+
image_file: Yüklenen görüntü dosyası.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
API'den alınan yanıt metni.
|
20 |
+
"""
|
21 |
try:
|
22 |
+
img = Image.open(image_file.name)
|
23 |
+
buffered = io.BytesIO()
|
24 |
+
img.save(buffered, format="PNG")
|
|
|
25 |
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
26 |
|
|
|
27 |
headers = {
|
28 |
"Content-Type": "application/json",
|
29 |
"Authorization": f"Bearer {API_KEY}",
|
|
|
49 |
}
|
50 |
|
51 |
response = requests.post(API_URL, headers=headers, json=payload)
|
52 |
+
response.raise_for_status() # Başarısız isteklerde hata fırlat
|
53 |
+
|
54 |
+
return response.json()["choices"][0]["message"]["content"]
|
55 |
|
56 |
+
except requests.exceptions.RequestException as e:
|
57 |
+
return f"API isteği hatası: {e}"
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
+
return f"Hata: {e}"
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
iface = gr.Interface(
|
63 |
+
fn=analyze_image,
|
64 |
+
inputs=gr.inputs.Image(type="file"),
|
|
|
65 |
outputs="text",
|
66 |
+
title="Ödeme Tutarı Analizi",
|
67 |
+
description="Bir görüntü yükleyin ve toplam ödeme tutarını öğrenin.",
|
68 |
)
|
69 |
|
70 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|