akhaliq HF staff commited on
Commit
5ee7ec4
1 Parent(s): c33dbd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import gradio as gr
 
 
2
  import os
3
  from together import Together
4
- from typing import List, Tuple
5
 
6
  # Initialize Together client
7
  client = Together()
@@ -10,9 +12,15 @@ client = Together()
10
  if "TOGETHER_API_KEY" not in os.environ:
11
  raise ValueError("Please set the TOGETHER_API_KEY environment variable")
12
 
13
- def call_llama_vision_api(prompt: str, image_url: str) -> str:
 
 
 
 
14
  getDescriptionPrompt = "You are a UX/UI designer. Describe the attached screenshot or UI mockup in detail. I will feed in the output you give me to a coding model that will attempt to recreate this mockup, so please think step by step and describe the UI in detail. Pay close attention to background color, text color, font size, font family, padding, margin, border, etc. Match the colors and sizes exactly. Make sure to mention every part of the screenshot including any headers, footers, etc. Use the exact text from the screenshot."
15
 
 
 
16
  messages = [
17
  {
18
  "role": "user",
@@ -21,7 +29,7 @@ def call_llama_vision_api(prompt: str, image_url: str) -> str:
21
  {
22
  "type": "image_url",
23
  "image_url": {
24
- "url": image_url,
25
  },
26
  },
27
  ],
@@ -38,33 +46,29 @@ def call_llama_vision_api(prompt: str, image_url: str) -> str:
38
  for chunk in stream:
39
  content = chunk.choices[0].delta.content or ""
40
  response += content
41
- yield response
42
 
43
- def chat(message: str, history: List[Tuple[str, str]], image_url: str) -> Tuple[str, List[Tuple[str, str]]]:
44
- if not message:
45
- return "", history
46
-
47
- full_response = ""
48
- for partial_response in call_llama_vision_api(message, image_url):
49
- full_response = partial_response
50
- yield "", history + [(message, full_response)]
 
51
 
52
- history.append((message, full_response))
53
- return "", history
54
 
55
- # Define the Gradio interface
56
  with gr.Blocks() as demo:
57
- gr.Markdown("# Llama 3.2 Vision Chatbot Demo")
58
- gr.Markdown("Enter your message and an image URL to analyze using the Llama 3.2 Vision model.")
59
 
60
- chatbot = gr.Chatbot()
61
- msg = gr.Textbox(label="Your message")
62
- image_url = gr.Textbox(label="Image URL", value="https://napkinsdev.s3.us-east-1.amazonaws.com/next-s3-uploads/d96a3145-472d-423a-8b79-bca3ad7978dd/trello-board.png")
63
-
64
- clear = gr.Button("Clear")
65
-
66
- msg.submit(chat, [msg, chatbot, image_url], [msg, chatbot])
67
- clear.click(lambda: None, None, chatbot, queue=False)
68
 
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
  import gradio as gr
2
+ from gradio_multimodalchatbot import MultimodalChatbot
3
+ from gradio.data_classes import FileData
4
  import os
5
  from together import Together
6
+ import base64
7
 
8
  # Initialize Together client
9
  client = Together()
 
12
  if "TOGETHER_API_KEY" not in os.environ:
13
  raise ValueError("Please set the TOGETHER_API_KEY environment variable")
14
 
15
+ def encode_image(image_path):
16
+ with open(image_path, "rb") as image_file:
17
+ return base64.b64encode(image_file.read()).decode('utf-8')
18
+
19
+ def call_llama_vision_api(prompt: str, image_path: str) -> str:
20
  getDescriptionPrompt = "You are a UX/UI designer. Describe the attached screenshot or UI mockup in detail. I will feed in the output you give me to a coding model that will attempt to recreate this mockup, so please think step by step and describe the UI in detail. Pay close attention to background color, text color, font size, font family, padding, margin, border, etc. Match the colors and sizes exactly. Make sure to mention every part of the screenshot including any headers, footers, etc. Use the exact text from the screenshot."
21
 
22
+ base64_image = encode_image(image_path)
23
+
24
  messages = [
25
  {
26
  "role": "user",
 
29
  {
30
  "type": "image_url",
31
  "image_url": {
32
+ "url": f"data:image/jpeg;base64,{base64_image}"
33
  },
34
  },
35
  ],
 
46
  for chunk in stream:
47
  content = chunk.choices[0].delta.content or ""
48
  response += content
49
+ return response
50
 
51
+ def chat(message, history):
52
+ user_message = message["text"]
53
+ files = message.get("files", [])
54
+
55
+ if files and files[0]["file"].path:
56
+ image_path = files[0]["file"].path
57
+ response = call_llama_vision_api(user_message, image_path)
58
+ else:
59
+ response = "I'm sorry, but I need an image to analyze. Please upload an image along with your question."
60
 
61
+ return {"text": response, "files": []}
 
62
 
 
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("# Llama 3.2 Vision Multimodal Chatbot Demo")
65
+ gr.Markdown("Upload an image and enter your message to analyze using the Llama 3.2 Vision model.")
66
 
67
+ chatbot = MultimodalChatbot(
68
+ value=[],
69
+ height=800,
70
+ callback=chat,
71
+ )
 
 
 
72
 
73
  if __name__ == "__main__":
74
  demo.launch()