Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from argparse import ArgumentParser
|
4 |
+
from groq import Groq
|
5 |
+
from PIL import Image
|
6 |
+
import base64
|
7 |
+
import io
|
8 |
+
|
9 |
+
# Initialize Groq client
|
10 |
+
API_KEY = os.environ['GROQ_API_KEY']
|
11 |
+
client = Groq(api_key=API_KEY)
|
12 |
+
|
13 |
+
REVISION = 'v1.0.4'
|
14 |
+
|
15 |
+
def _get_args():
|
16 |
+
parser = ArgumentParser()
|
17 |
+
parser.add_argument("--revision", type=str, default=REVISION)
|
18 |
+
parser.add_argument("--share", action="store_true", default=False, help="Create a publicly shareable link for the interface.")
|
19 |
+
return parser.parse_args()
|
20 |
+
|
21 |
+
def process_image(image):
|
22 |
+
# Convert image to bytes for Groq API
|
23 |
+
buffered = io.BytesIO()
|
24 |
+
image.save(buffered, format="JPEG")
|
25 |
+
return buffered.getvalue()
|
26 |
+
|
27 |
+
def create_messages(query, image_data):
|
28 |
+
messages = []
|
29 |
+
|
30 |
+
# User query as text
|
31 |
+
if query:
|
32 |
+
messages.append({'role': 'user', 'content': query})
|
33 |
+
|
34 |
+
# Include image if provided
|
35 |
+
if image_data:
|
36 |
+
image_base64 = f"data:image/jpeg;base64,{base64.b64encode(image_data).decode()}"
|
37 |
+
messages.append({
|
38 |
+
'role': 'user',
|
39 |
+
'content': [
|
40 |
+
{"type": "text", "text": "Please analyze this image."},
|
41 |
+
{"type": "image_url", "image_url": {"url": image_base64}}
|
42 |
+
]
|
43 |
+
})
|
44 |
+
|
45 |
+
return messages
|
46 |
+
|
47 |
+
def predict(chat_history, query, image):
|
48 |
+
# Process the image if provided
|
49 |
+
image_data = process_image(image) if image else None
|
50 |
+
messages = create_messages(query, image_data)
|
51 |
+
|
52 |
+
# Call the Groq API with the messages
|
53 |
+
try:
|
54 |
+
completion = client.chat.completions.create(
|
55 |
+
model="llama-3.2-11b-vision-preview",
|
56 |
+
messages=messages,
|
57 |
+
temperature=1,
|
58 |
+
max_tokens=1500,
|
59 |
+
top_p=1,
|
60 |
+
stream=False,
|
61 |
+
)
|
62 |
+
|
63 |
+
response_text = completion.choices[0].message.content.strip()
|
64 |
+
except Exception as e:
|
65 |
+
response_text = f"Error: {str(e)}"
|
66 |
+
|
67 |
+
chat_history.append((query, response_text))
|
68 |
+
return chat_history
|
69 |
+
|
70 |
+
def clear_history():
|
71 |
+
return []
|
72 |
+
|
73 |
+
def main():
|
74 |
+
args = _get_args()
|
75 |
+
|
76 |
+
with gr.Blocks() as demo:
|
77 |
+
gr.Markdown("<h1 style='text-align: center;'>Llama-3.2-11b-vision-preview</h1>")
|
78 |
+
|
79 |
+
chatbox = gr.Chatbot()
|
80 |
+
query = gr.Textbox(label="Input", placeholder="Type your query here...")
|
81 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
82 |
+
|
83 |
+
submit_btn = gr.Button("Submit")
|
84 |
+
clear_btn = gr.Button("Clear History")
|
85 |
+
|
86 |
+
submit_btn.click(predict, inputs=[chatbox, query, image_input], outputs=chatbox)
|
87 |
+
clear_btn.click(clear_history, outputs=chatbox)
|
88 |
+
|
89 |
+
demo.launch(share=args.share)
|
90 |
+
|
91 |
+
if __name__ == '__main__':
|
92 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
groq
|
3 |
+
Pillow
|