gojossatoru commited on
Commit
d46df35
1 Parent(s): 38d9987

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -53
app.py CHANGED
@@ -1,27 +1,29 @@
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}",
@@ -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
- 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.Image(type="pil"), # Updated to use gr.Image
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
- 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()