Commit
•
8998372
1
Parent(s):
3719278
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import json
|
8 |
+
|
9 |
+
def resize_image(image_path, max_size=(800, 800), quality=85):
|
10 |
+
with Image.open(image_path) as img:
|
11 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
12 |
+
buffer = io.BytesIO()
|
13 |
+
img.save(buffer, format="JPEG", quality=quality)
|
14 |
+
return buffer.getvalue()
|
15 |
+
|
16 |
+
def filepath_to_base64(image_path):
|
17 |
+
img_bytes = resize_image(image_path)
|
18 |
+
img_base64 = base64.b64encode(img_bytes)
|
19 |
+
return img_base64.decode('utf-8')
|
20 |
+
|
21 |
+
api_key = os.getenv('API_KEY')
|
22 |
+
|
23 |
+
def call_deplot_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
|
24 |
+
image_base64 = filepath_to_base64(image_path)
|
25 |
+
invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/3bc390c7-eeec-40f7-a64d-0c6a719985f7"
|
26 |
+
headers = {
|
27 |
+
"Authorization": f"Bearer {api_key}",
|
28 |
+
"Accept": "application/json",
|
29 |
+
}
|
30 |
+
|
31 |
+
payload = {
|
32 |
+
"messages": [
|
33 |
+
{
|
34 |
+
"content": f"{content} <img src=\"data:image/jpeg;base64,{image_base64}\" />",
|
35 |
+
"role": "user"
|
36 |
+
}
|
37 |
+
],
|
38 |
+
"temperature": temperature,
|
39 |
+
"top_p": top_p,
|
40 |
+
"max_tokens": max_tokens,
|
41 |
+
"stream": False
|
42 |
+
}
|
43 |
+
|
44 |
+
session = requests.Session()
|
45 |
+
response = session.post(invoke_url, headers=headers, json=payload)
|
46 |
+
|
47 |
+
while response.status_code == 202:
|
48 |
+
request_id = response.headers.get("NVCF-REQID")
|
49 |
+
fetch_url = f"https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/{request_id}"
|
50 |
+
response = session.get(fetch_url, headers=headers)
|
51 |
+
|
52 |
+
response.raise_for_status()
|
53 |
+
response_body = response.json()
|
54 |
+
print(response_body)
|
55 |
+
|
56 |
+
# Processar a resposta conforme necessário
|
57 |
+
# Este exemplo apenas imprime a resposta, mas você pode adaptá-lo conforme necessário
|
58 |
+
return response_body
|
59 |
+
|
60 |
+
# Configuração da Interface Gráfica
|
61 |
+
content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
|
62 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
63 |
+
temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
|
64 |
+
top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
|
65 |
+
max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
|
66 |
+
|
67 |
+
iface = gr.Interface(fn=call_deplot_api,
|
68 |
+
inputs=[image_input, content_input, temperature_input, top_p_input, max_tokens_input],
|
69 |
+
outputs="text",
|
70 |
+
title="Google DePlot API Explorer",
|
71 |
+
description="""
|
72 |
+
<div style="text-align: center; font-size: 1.5em; margin-bottom: 20px;">
|
73 |
+
<strong>Explore Visual Language Understanding with Google DePlot</strong>
|
74 |
+
</div>
|
75 |
+
<p>
|
76 |
+
Utilize Google DePlot to translate images of plots or charts into linearized tables. This one-shot visual language understanding solution offers a unique approach to interpreting visual data.
|
77 |
+
</p>
|
78 |
+
...
|
79 |
+
"""
|
80 |
+
)
|
81 |
+
|
82 |
+
iface.launch()
|