Spaces:
Sleeping
Sleeping
Commit
•
28050f6
1
Parent(s):
796a417
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,18 @@ import base64
|
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def filepath_to_base64(image_path):
|
10 |
-
|
11 |
-
|
12 |
-
return f"data:image/
|
13 |
|
14 |
# A chave da API é lida de uma variável de ambiente para maior segurança.
|
15 |
api_key = os.getenv('API_KEY')
|
@@ -28,12 +36,11 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
|
|
28 |
"accept": "text/event-stream",
|
29 |
"content-type": "application/json",
|
30 |
}
|
31 |
-
# Gera uma seed aleatória se nenhuma for fornecida pelo usuário.
|
32 |
if seed is None or seed == "":
|
33 |
seed = random.randint(0, 18446744073709552000)
|
34 |
else:
|
35 |
try:
|
36 |
-
seed = int(seed)
|
37 |
except ValueError:
|
38 |
return "Seed must be an integer."
|
39 |
|
@@ -55,11 +62,9 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
|
|
55 |
if response.status_code != 200:
|
56 |
print(f"Erro na requisição: {response.status_code}")
|
57 |
try:
|
58 |
-
# Tenta imprimir a mensagem de erro detalhada, se disponível
|
59 |
error_details = response.json()
|
60 |
print(error_details)
|
61 |
except ValueError:
|
62 |
-
# Se a resposta de erro não for um JSON, imprime o texto bruto
|
63 |
print(response.text)
|
64 |
else:
|
65 |
print(response)
|
@@ -69,7 +74,6 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
|
|
69 |
decoded_line = line.decode("utf-8")
|
70 |
if "content" in decoded_line:
|
71 |
response_text += decoded_line.split('"content":"')[1].split('","finish_reason')[0]
|
72 |
-
|
73 |
return response_text
|
74 |
|
75 |
# Definindo os componentes da interface
|
@@ -78,7 +82,7 @@ image_input = gr.Image(type="filepath", label="Upload Image")
|
|
78 |
temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
|
79 |
top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
|
80 |
max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
|
81 |
-
seed_input = gr.Textbox(label="Seed (optional)")
|
82 |
|
83 |
# Criando a interface Gradio
|
84 |
iface = gr.Interface(fn=call_fuyu_8b_api,
|
@@ -88,4 +92,4 @@ iface = gr.Interface(fn=call_fuyu_8b_api,
|
|
88 |
description="Explore the capabilities of Fuyu-8B multi-modal transformer.")
|
89 |
|
90 |
# Executando a interface
|
91 |
-
iface.launch()
|
|
|
6 |
from PIL import Image
|
7 |
import io
|
8 |
|
9 |
+
|
10 |
+
def resize_image(image_path, max_size=(800, 800), quality=85):
|
11 |
+
with Image.open(image_path) as img:
|
12 |
+
img.thumbnail(max_size, Image.ANTIALIAS)
|
13 |
+
buffer = io.BytesIO()
|
14 |
+
img.save(buffer, format="JPEG", quality=quality)
|
15 |
+
return buffer.getvalue()
|
16 |
+
|
17 |
def filepath_to_base64(image_path):
|
18 |
+
img_bytes = resize_image(image_path)
|
19 |
+
img_base64 = base64.b64encode(img_bytes)
|
20 |
+
return f"data:image/jpeg;base64,{img_base64.decode('utf-8')}"
|
21 |
|
22 |
# A chave da API é lida de uma variável de ambiente para maior segurança.
|
23 |
api_key = os.getenv('API_KEY')
|
|
|
36 |
"accept": "text/event-stream",
|
37 |
"content-type": "application/json",
|
38 |
}
|
|
|
39 |
if seed is None or seed == "":
|
40 |
seed = random.randint(0, 18446744073709552000)
|
41 |
else:
|
42 |
try:
|
43 |
+
seed = int(seed)
|
44 |
except ValueError:
|
45 |
return "Seed must be an integer."
|
46 |
|
|
|
62 |
if response.status_code != 200:
|
63 |
print(f"Erro na requisição: {response.status_code}")
|
64 |
try:
|
|
|
65 |
error_details = response.json()
|
66 |
print(error_details)
|
67 |
except ValueError:
|
|
|
68 |
print(response.text)
|
69 |
else:
|
70 |
print(response)
|
|
|
74 |
decoded_line = line.decode("utf-8")
|
75 |
if "content" in decoded_line:
|
76 |
response_text += decoded_line.split('"content":"')[1].split('","finish_reason')[0]
|
|
|
77 |
return response_text
|
78 |
|
79 |
# Definindo os componentes da interface
|
|
|
82 |
temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
|
83 |
top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
|
84 |
max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
|
85 |
+
seed_input = gr.Textbox(label="Seed (optional)")
|
86 |
|
87 |
# Criando a interface Gradio
|
88 |
iface = gr.Interface(fn=call_fuyu_8b_api,
|
|
|
92 |
description="Explore the capabilities of Fuyu-8B multi-modal transformer.")
|
93 |
|
94 |
# Executando a interface
|
95 |
+
iface.launch()
|