Spaces:
Building
Building
from fastapi import FastAPI, UploadFile, File, HTTPException | |
from fastapi.responses import JSONResponse, PlainTextResponse | |
from pyngrok import ngrok | |
import base64 | |
import requests | |
from io import BytesIO | |
from PIL import Image | |
import gradio as gr | |
app = FastAPI() | |
API_URL = "https://api.hyperbolic.xyz/v1/chat/completions" | |
API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJnb2pvNzk0ODRAZ21haWwuY29tIn0.J4bPeGA4-6tHXYNz1FHi9WSJ0gY_7MW2E9m28aGBh6g" | |
async def upload_image(file: UploadFile = File(...)): | |
try: | |
# Read and convert the image to base64 | |
img = Image.open(BytesIO(await file.read())) | |
buffered = BytesIO() | |
img.save(buffered, format="PNG") # Save in PNG format | |
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8") | |
# Send request to the API | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {API_KEY}", | |
} | |
payload = { | |
"messages": [ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": "Toplam ödeme tutarı ne kadardır?"}, | |
{ | |
"type": "image_url", | |
"image_url": {"url": f"data:image/png;base64,{encoded_string}"}, | |
}, | |
], | |
} | |
], | |
"model": "Qwen/Qwen2-VL-72B-Instruct", | |
"max_tokens": 2048, | |
"temperature": 0.7, | |
"top_p": 0.9, | |
} | |
response = requests.post(API_URL, headers=headers, json=payload) | |
if response.status_code == 200: | |
content = response.json()["choices"][0]["message"]["content"] | |
return PlainTextResponse(content) # Return as plain text | |
else: | |
return JSONResponse(content={"error": response.json()}, status_code=response.status_code) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
def process_image(image): | |
# Convert the Gradio image input to a format suitable for FastAPI | |
buffered = BytesIO() | |
image.save(buffered, format="PNG") | |
buffered.seek(0) | |
# Create an UploadFile-like object | |
file_like = UploadFile( | |
filename="image.png", | |
file=buffered, | |
content_type="image/png" | |
) | |
return upload_image(file_like) | |
# Gradio interface | |
gr_interface = gr.Interface( | |
fn=process_image, | |
inputs=gr.inputs.Image(type="pil"), | |
outputs="text", | |
title="Image Upload", | |
description="Upload an image to get information about the payment amount." | |
) | |
if __name__ == "__main__": | |
ngrok.set_auth_token("2m46ThDvL9VaQVxeVKs47yt9VRb_66BX7RzbfnQwWUj1cQeoV") | |
public_url = ngrok.connect(8000) | |
print(f"Ngrok URL: {public_url}") | |
# Launch Gradio app | |
gr_interface.launch(share=True) | |
# Start FastAPI | |
import uvicorn | |
uvicorn.run(app, port=8000) |