hassanelmghari commited on
Commit
20b1f08
·
verified ·
1 Parent(s): f75a734

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -26
app.py CHANGED
@@ -31,7 +31,7 @@ def encode_image(image_path, max_size=(800, 800), quality=85):
31
  img.save(buffered, format="JPEG", quality=quality)
32
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
33
 
34
- def bot_streaming(message, history, max_new_tokens=250, api_key=None, max_history=5):
35
  if client is None:
36
  initialize_client(api_key)
37
 
@@ -73,36 +73,41 @@ def bot_streaming(message, history, max_new_tokens=250, api_key=None, max_histor
73
  time.sleep(0.01)
74
  yield buffer
75
 
76
- except together.error.InvalidRequestError as e:
77
  if "Request Entity Too Large" in str(e):
78
  yield "The image is too large. Please try with a smaller image or compress the existing one."
79
  else:
80
  yield f"An error occurred: {str(e)}"
81
 
82
- demo = gr.ChatInterface(
83
- fn=bot_streaming,
84
- title="Meta Llama-3.2-11B-Vision-Instruct (FREE)",
85
- textbox=gr.MultimodalTextbox(),
86
- additional_inputs=[
87
- gr.Textbox(
88
- label="Together API Key",
89
- placeholder="Enter your API key here.",
90
- required=True
91
- ),
92
- gr.Slider(
93
- minimum=10,
94
- maximum=500,
95
- value=250,
96
- step=10,
97
- label="Maximum number of new tokens to generate",
98
- )
99
- ],
100
- cache_examples=False,
101
- description="Try the new Llama 3.2 11B Vision API by Meta for free through Together AI. Upload an image, and start chatting about it. Just paste in your Together AI API key and get started!",
102
- stop_btn="Stop Generation",
103
- fill_height=True,
104
- multimodal=True
105
- )
 
 
 
 
 
106
 
107
  if __name__ == "__main__":
108
  demo.launch(debug=True)
 
31
  img.save(buffered, format="JPEG", quality=quality)
32
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
33
 
34
+ def bot_streaming(message, history, api_key, max_new_tokens=250, max_history=5):
35
  if client is None:
36
  initialize_client(api_key)
37
 
 
73
  time.sleep(0.01)
74
  yield buffer
75
 
76
+ except Exception as e:
77
  if "Request Entity Too Large" in str(e):
78
  yield "The image is too large. Please try with a smaller image or compress the existing one."
79
  else:
80
  yield f"An error occurred: {str(e)}"
81
 
82
+ with gr.Blocks() as demo:
83
+ gr.Markdown("# Meta Llama-3.2-11B-Vision-Instruct (FREE)")
84
+ gr.Markdown("Try the new Llama 3.2 11B Vision API by Meta for free through Together AI. Upload an image, and start chatting about it. Just paste in your Together AI API key and get started!")
85
+
86
+ api_key = gr.Textbox(
87
+ label="Together API Key",
88
+ placeholder="Enter your API key here.",
89
+ type="password"
90
+ )
91
+
92
+ chatbot = gr.ChatInterface(
93
+ fn=bot_streaming,
94
+ textbox=gr.MultimodalTextbox(),
95
+ additional_inputs=[
96
+ gr.Slider(
97
+ minimum=10,
98
+ maximum=500,
99
+ value=250,
100
+ step=10,
101
+ label="Maximum number of new tokens to generate",
102
+ )
103
+ ],
104
+ cache_examples=False,
105
+ stop_btn="Stop Generation",
106
+ fill_height=True,
107
+ multimodal=True
108
+ )
109
+
110
+ api_key.change(lambda x: x, inputs=[api_key], outputs=[chatbot.additional_inputs[0]])
111
 
112
  if __name__ == "__main__":
113
  demo.launch(debug=True)