feat: add demo
Browse files
demo.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import base64
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
ENDPOINT_URL = "https://api.runpod.ai/v2/qkqui1t394hjws/runsync"
|
11 |
+
API_KEY = os.getenv("API_KEY")
|
12 |
+
|
13 |
+
def encode_image_to_base64(image):
|
14 |
+
buffered = BytesIO()
|
15 |
+
image.save(buffered, format="JPEG")
|
16 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
17 |
+
return img_str
|
18 |
+
|
19 |
+
|
20 |
+
def critic_image(image):
|
21 |
+
img_base64 = encode_image_to_base64(image)
|
22 |
+
payload = {
|
23 |
+
"input": {
|
24 |
+
"max_new_tokens": 512,
|
25 |
+
"category": "General Visual Analysis",
|
26 |
+
"image": img_base64
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
headers = {
|
31 |
+
"Authorization": API_KEY,
|
32 |
+
"Content-Type": "application/json"
|
33 |
+
}
|
34 |
+
|
35 |
+
|
36 |
+
response = requests.post(ENDPOINT_URL, json=payload, headers=headers)
|
37 |
+
result = response.json()
|
38 |
+
|
39 |
+
return result['output']['result'].strip()
|
40 |
+
|
41 |
+
|
42 |
+
demo = gr.Interface(
|
43 |
+
fn=critic_image,
|
44 |
+
inputs=gr.Image(type="pil"),
|
45 |
+
outputs="text",
|
46 |
+
title="Gemmarte",
|
47 |
+
description="Upload an image and get a visual analysis in text form from the Gemmarte model."
|
48 |
+
)
|
49 |
+
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.launch()
|