Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import os | |
import random | |
import base64 | |
from PIL import Image | |
import io | |
def resize_image(image_path, max_size=(800, 800), quality=85): | |
with Image.open(image_path) as img: | |
img.thumbnail(max_size, Image.ANTIALIAS) | |
buffer = io.BytesIO() | |
img.save(buffer, format="JPEG", quality=quality) | |
return buffer.getvalue() | |
def filepath_to_base64(image_path): | |
img_bytes = resize_image(image_path) | |
img_base64 = base64.b64encode(img_bytes) | |
return f"data:image/jpeg;base64,{img_base64.decode('utf-8')}" | |
# A chave da API é lida de uma variável de ambiente para maior segurança. | |
api_key = os.getenv('API_KEY') | |
def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024, seed=None): | |
print(f"Caminho da imagem recebida: {image_path}") | |
print(f"Conteúdo: {content}") | |
print(f"Temperatura: {temperature}") | |
print(f"Top P: {top_p}") | |
print(f"Max Tokens: {max_tokens}") | |
print(f"Seed: {seed}") | |
image_base64 = filepath_to_base64(image_path) | |
invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/9f757064-657f-4c85-abd7-37a7a9b6ee11" | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"accept": "text/event-stream", | |
"content-type": "application/json", | |
} | |
if seed is None or seed == "": | |
seed = random.randint(0, 18446744073709552000) | |
else: | |
try: | |
seed = int(seed) | |
except ValueError: | |
return "Seed must be an integer." | |
payload = { | |
"messages": [ | |
{ | |
"content": f"{content} <img src=\"{image_base64}\" />", | |
"role": "user" | |
} | |
], | |
"temperature": temperature, | |
"top_p": top_p, | |
"max_tokens": max_tokens, | |
"stream": True, | |
"seed": seed | |
} | |
print(payload) | |
response = requests.post(invoke_url, headers=headers, json=payload, stream=True) | |
if response.status_code != 200: | |
print(f"Erro na requisição: {response.status_code}") | |
try: | |
error_details = response.json() | |
print(error_details) | |
except ValueError: | |
print(response.text) | |
else: | |
print(response) | |
response_text = "" | |
for line in response.iter_lines(): | |
if line: | |
decoded_line = line.decode("utf-8") | |
if "content" in decoded_line: | |
response_text += decoded_line.split('"content":"')[1].split('","finish_reason')[0] | |
return response_text | |
# Definindo os componentes da interface | |
content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content") | |
image_input = gr.Image(type="filepath", label="Upload Image") | |
temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature") | |
top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P") | |
max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens") | |
seed_input = gr.Textbox(label="Seed (optional)") | |
# Criando a interface Gradio | |
iface = gr.Interface(fn=call_fuyu_8b_api, | |
inputs=[image_input, content_input, temperature_input, top_p_input, max_tokens_input, seed_input], | |
outputs="text", | |
title="Fuyu-8B API Explorer", | |
description="Explore the capabilities of Fuyu-8B multi-modal transformer.") | |
# Executando a interface | |
iface.launch() |