artificialguybr commited on
Commit
1f986fb
1 Parent(s): ab6046d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -36
app.py CHANGED
@@ -5,14 +5,19 @@ 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)
@@ -20,13 +25,8 @@ def filepath_to_base64(image_path):
20
 
21
  api_key = os.getenv('API_KEY')
22
 
 
23
  def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens=1024):
24
- print(f"Caminho da imagem recebida: {image_path}")
25
- print(f"Conteúdo: {content}")
26
- print(f"Temperatura: {temperature}")
27
- print(f"Top P: {top_p}")
28
- print(f"Max Tokens: {max_tokens}")
29
-
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 = {
@@ -56,39 +56,52 @@ def call_fuyu_8b_api(image_path, content, temperature=0.2, top_p=0.7, max_tokens
56
  print(error_details)
57
  except ValueError:
58
  print(response.text)
 
59
  else:
60
  response_text = ""
61
  for line in response.iter_lines():
62
  if line:
63
- try:
64
- # Decode the line from bytes to string
65
- decoded_line = line.decode('utf-8')
66
-
67
- # Remove the "data: " prefix
68
- if decoded_line.startswith('data: '):
69
- json_str = decoded_line[6:] # Remove the first 6 characters ('data: ')
70
- json_line = json.loads(json_str)
71
-
72
- # Assuming the structure is consistent with the examples you provided.
73
- content_parts = json_line.get("choices", [{}])[0].get("delta", {}).get("content", "")
74
- response_text += content_parts
75
- else:
76
- print(f"Unexpected line format: {decoded_line}")
77
- except json.JSONDecodeError as e:
78
- print(f"Error decoding JSON from response line: {e}")
79
- print(f"Faulty line: {line}")
80
-
81
  return response_text
82
- content_input = gr.Textbox(lines=2, placeholder="Enter your content here...", label="Content")
83
- image_input = gr.Image(type="filepath", label="Upload Image")
84
- temperature_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.2, label="Temperature")
85
- top_p_input = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Top P")
86
- max_tokens_input = gr.Slider(minimum=1, maximum=1024, step=1, value=1024, label="Max Tokens")
87
 
88
- iface = gr.Interface(fn=call_fuyu_8b_api,
89
- inputs=[image_input, content_input, temperature_input, top_p_input, max_tokens_input],
90
- outputs="text",
91
- title="Fuyu-8B API Explorer",
92
- description="Explore the capabilities of Fuyu-8B multi-modal transformer.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- iface.launch()
 
5
  from PIL import Image
6
  import io
7
  import json
8
+ import copy
9
+ import secrets
10
+ from pathlib import Path
11
+ import re
12
 
13
+ # Funções auxiliares
14
  def resize_image(image_path, max_size=(800, 800), quality=85):
15
  with Image.open(image_path) as img:
16
  img.thumbnail(max_size, Image.Resampling.LANCZOS)
17
  buffer = io.BytesIO()
18
  img.save(buffer, format="JPEG", quality=quality)
19
  return buffer.getvalue()
20
+
21
  def filepath_to_base64(image_path):
22
  img_bytes = resize_image(image_path)
23
  img_base64 = base64.b64encode(img_bytes)
 
25
 
26
  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 = {
 
56
  print(error_details)
57
  except ValueError:
58
  print(response.text)
59
+ return "Error in API call"
60
  else:
61
  response_text = ""
62
  for line in response.iter_lines():
63
  if line:
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
73
+ def update_chatbot_with_response(chatbot, response):
74
+ chatbot.append(("Assistant", response))
75
+
76
+ def submit_response(chatbot, task_history, image_input, content_input, temperature_input, top_p_input, max_tokens_input):
77
+ image_path = image_input if image_input else "No image provided"
78
+ response = call_fuyu_8b_api(image_path, content_input, temperature_input, top_p_input, max_tokens_input)
79
+ update_chatbot_with_response(chatbot, response)
80
+ return chatbot, task_history
81
+
82
+ def reset_state(task_history):
83
+ task_history.clear()
84
+ return [], task_history
85
+
86
+ css = '''
87
+ .gradio-container{max-width:800px !important}
88
+ '''
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():
95
+ addfile_btn = gr.File(label="📁 Upload Image", file_types=["jpg", "png"])
96
+ submit_btn = gr.Button("🚀 Submit")
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()