artificialguybr commited on
Commit
ccd71e8
1 Parent(s): a6b60cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -25
app.py CHANGED
@@ -1,12 +1,10 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- import random
5
  import base64
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.Resampling.LANCZOS)
@@ -19,28 +17,14 @@ def filepath_to_base64(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')
24
 
25
- def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024, seed=None):
26
  print(f"Caminho da imagem recebida: {image_path}")
27
  print(f"Conteúdo: {content}")
28
  print(f"Temperatura: {temperature}")
29
  print(f"Top P: {top_p}")
30
  print(f"Max Tokens: {max_tokens}")
31
-
32
- # Convertendo e validando o seed
33
- if seed is not None:
34
- try:
35
- seed = int(seed)
36
- print(f"Seed utilizado: {seed}")
37
- except ValueError:
38
- # Se o seed fornecido não puder ser convertido para inteiro, retorna erro
39
- return "Seed must be an integer."
40
- else:
41
- # Gera um seed aleatório se nenhum for fornecido
42
- seed = random.randint(0, 18446744073709551615)
43
- print(f"Seed gerado aleatoriamente: {seed}")
44
 
45
  image_base64 = filepath_to_base64(image_path)
46
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/9f757064-657f-4c85-abd7-37a7a9b6ee11"
@@ -60,8 +44,7 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
60
  "temperature": temperature,
61
  "top_p": top_p,
62
  "max_tokens": max_tokens,
63
- "stream": True,
64
- "seed": seed
65
  }
66
 
67
  response = requests.post(invoke_url, headers=headers, json=payload, stream=True)
@@ -81,20 +64,17 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
81
  response_text += decoded_line.split('"content":"')[1].split('","finish_reason')[0]
82
 
83
  return response_text
84
- # Definindo os componentes da interface
85
  content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
86
  image_input = gr.Image(type="filepath", label="Upload Image")
87
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
88
  top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
89
  max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
90
- seed_input = gr.Textbox(label="Seed (optional)")
91
 
92
- # Criando a interface Gradio
93
  iface = gr.Interface(fn=call_fuyu_8b_api,
94
- inputs=[image_input, content_input, temperature_input, top_p_input, max_tokens_input, seed_input],
95
  outputs="text",
96
  title="Fuyu-8B API Explorer",
97
  description="Explore the capabilities of Fuyu-8B multi-modal transformer.")
98
 
99
- # Executando a interface
100
- iface.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
 
4
  import base64
5
  from PIL import Image
6
  import io
7
 
 
8
  def resize_image(image_path, max_size=(800, 800), quality=85):
9
  with Image.open(image_path) as img:
10
  img.thumbnail(max_size, Image.Resampling.LANCZOS)
 
17
  img_base64 = base64.b64encode(img_bytes)
18
  return f"data:image/jpeg;base64,{img_base64.decode('utf-8')}"
19
 
 
20
  api_key = os.getenv('API_KEY')
21
 
22
+ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
23
  print(f"Caminho da imagem recebida: {image_path}")
24
  print(f"Conteúdo: {content}")
25
  print(f"Temperatura: {temperature}")
26
  print(f"Top P: {top_p}")
27
  print(f"Max Tokens: {max_tokens}")
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  image_base64 = filepath_to_base64(image_path)
30
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/9f757064-657f-4c85-abd7-37a7a9b6ee11"
 
44
  "temperature": temperature,
45
  "top_p": top_p,
46
  "max_tokens": max_tokens,
47
+ "stream": True
 
48
  }
49
 
50
  response = requests.post(invoke_url, headers=headers, json=payload, stream=True)
 
64
  response_text += decoded_line.split('"content":"')[1].split('","finish_reason')[0]
65
 
66
  return response_text
67
+
68
  content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
69
  image_input = gr.Image(type="filepath", label="Upload Image")
70
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
71
  top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
72
  max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
 
73
 
 
74
  iface = gr.Interface(fn=call_fuyu_8b_api,
75
+ inputs=[image_input, content_input, temperature_input, top_p_input, max_tokens_input],
76
  outputs="text",
77
  title="Fuyu-8B API Explorer",
78
  description="Explore the capabilities of Fuyu-8B multi-modal transformer.")
79
 
80
+ iface.launch()