x
File size: 3,004 Bytes
4f92651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
449f10c
4f92651
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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"

@app.post("/upload-image/")
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)