artificialguybr commited on
Commit
dae2bd7
·
verified ·
1 Parent(s): 1f986fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -27,7 +27,9 @@ api_key = os.getenv('API_KEY')
27
 
28
  # Função principal da API
29
  def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
30
- image_base64 = filepath_to_base64(image_path)
 
 
31
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/9f757064-657f-4c85-abd7-37a7a9b6ee11"
32
  headers = {
33
  "Authorization": f"Bearer {api_key}",
@@ -38,7 +40,7 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
38
  payload = {
39
  "messages": [
40
  {
41
- "content": f"{content} <img src=\"{image_base64}\" />",
42
  "role": "user"
43
  }
44
  ],
@@ -64,9 +66,10 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
64
  decoded_line = line.decode('utf-8')
65
  if decoded_line.startswith('data: '):
66
  json_str = decoded_line[6:]
67
- json_line = json.loads(json_str)
68
- content_parts = json_line.get("choices", [{}])[0].get("delta", {}).get("content", "")
69
- response_text += content_parts
 
70
  return response_text
71
 
72
  # Interface Gradio
@@ -89,6 +92,7 @@ css = '''
89
 
90
  with gr.Blocks(css=css) as demo:
91
  gr.Markdown("# Fuyu-8B API Explorer with Enhanced Features")
 
92
  chatbot = gr.Chatbot(label='Chatbot', elem_classes="control-height", height=520)
93
  task_history = gr.State([])
94
  with gr.Row():
@@ -97,11 +101,10 @@ with gr.Blocks(css=css) as demo:
97
  regen_btn = gr.Button("🤔 Regenerate")
98
  empty_bin = gr.Button("🧹 Clear History")
99
  with gr.Accordion("Advanced Settings"):
100
- content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
101
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
102
  top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
103
  max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
104
  submit_btn.click(submit_response, [chatbot, task_history, addfile_btn, content_input, temperature_input, top_p_input, max_tokens_input], [chatbot, task_history])
105
  empty_bin.click(reset_state, [task_history], [chatbot, task_history])
106
 
107
- demo.launch()
 
27
 
28
  # Função principal da API
29
  def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
30
+ if image_path != "No image provided":
31
+ image_base64 = filepath_to_base64(image_path)
32
+ content = f"{content} <img src=\"{image_base64}\" />"
33
  invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/9f757064-657f-4c85-abd7-37a7a9b6ee11"
34
  headers = {
35
  "Authorization": f"Bearer {api_key}",
 
40
  payload = {
41
  "messages": [
42
  {
43
+ "content": content,
44
  "role": "user"
45
  }
46
  ],
 
66
  decoded_line = line.decode('utf-8')
67
  if decoded_line.startswith('data: '):
68
  json_str = decoded_line[6:]
69
+ if json_str: # Verifica se json_str não está vazio
70
+ json_line = json.loads(json_str)
71
+ content_parts = json_line.get("choices", [{}])[0].get("delta", {}).get("content", "")
72
+ response_text += content_parts
73
  return response_text
74
 
75
  # Interface Gradio
 
92
 
93
  with gr.Blocks(css=css) as demo:
94
  gr.Markdown("# Fuyu-8B API Explorer with Enhanced Features")
95
+ content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
96
  chatbot = gr.Chatbot(label='Chatbot', elem_classes="control-height", height=520)
97
  task_history = gr.State([])
98
  with gr.Row():
 
101
  regen_btn = gr.Button("🤔 Regenerate")
102
  empty_bin = gr.Button("🧹 Clear History")
103
  with gr.Accordion("Advanced Settings"):
 
104
  temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
105
  top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
106
  max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
107
  submit_btn.click(submit_response, [chatbot, task_history, addfile_btn, content_input, temperature_input, top_p_input, max_tokens_input], [chatbot, task_history])
108
  empty_bin.click(reset_state, [task_history], [chatbot, task_history])
109
 
110
+ demo.launch()