Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,12 +10,16 @@ groq_api_key = "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"
|
|
10 |
groq_api_url = "https://api.groq.com/openai/v1/chat/completions"
|
11 |
|
12 |
def qna(image, question, history):
|
|
|
|
|
|
|
13 |
try:
|
14 |
inputs = processor(image, question, return_tensors="pt")
|
15 |
out = model.generate(**inputs)
|
16 |
short_answer = processor.decode(out[0], skip_special_tokens=True)
|
17 |
|
18 |
-
context = "\n".join([f"Q: {q}\nA: {a}" for q, a in history])
|
|
|
19 |
full_prompt = f"""Context of previous conversation:
|
20 |
{context}
|
21 |
|
@@ -30,30 +34,35 @@ Please provide a detailed answer based on the image and previous context."""
|
|
30 |
|
31 |
data = {
|
32 |
"model": "llama3-8b-8192",
|
33 |
-
"messages": [
|
|
|
|
|
|
|
34 |
}
|
35 |
|
36 |
response = requests.post(groq_api_url, headers=headers, json=data)
|
37 |
|
38 |
if response.status_code == 200:
|
39 |
detailed_answer = response.json()['choices'][0]['message']['content'].strip()
|
40 |
-
history
|
41 |
-
return
|
42 |
else:
|
43 |
error_msg = f"Error {response.status_code}: {response.text}"
|
44 |
-
history
|
45 |
-
return history, history
|
46 |
|
47 |
except Exception as e:
|
48 |
error_msg = f"An error occurred: {str(e)}"
|
49 |
-
history
|
50 |
-
return history, history
|
51 |
|
52 |
def clear_history():
|
53 |
return [], []
|
54 |
|
|
|
|
|
|
|
55 |
with gr.Blocks() as demo:
|
56 |
gr.Markdown("# Interactive Image Chatbot")
|
|
|
57 |
|
58 |
with gr.Row():
|
59 |
image_input = gr.Image(type="pil")
|
@@ -61,21 +70,37 @@ with gr.Blocks() as demo:
|
|
61 |
with gr.Row():
|
62 |
with gr.Column():
|
63 |
chatbot = gr.Chatbot()
|
64 |
-
question = gr.Textbox(label="Ask a question about the image")
|
65 |
-
|
|
|
|
|
66 |
|
67 |
state = gr.State([])
|
68 |
|
|
|
69 |
question.submit(
|
70 |
qna,
|
71 |
inputs=[image_input, question, state],
|
72 |
outputs=[chatbot, state]
|
73 |
)
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
clear.click(
|
76 |
clear_history,
|
77 |
outputs=[chatbot, state]
|
78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
demo.launch()
|
|
|
10 |
groq_api_url = "https://api.groq.com/openai/v1/chat/completions"
|
11 |
|
12 |
def qna(image, question, history):
|
13 |
+
if image is None:
|
14 |
+
return history + [(question, "Please upload an image first.")], history + [(question, "Please upload an image first.")]
|
15 |
+
|
16 |
try:
|
17 |
inputs = processor(image, question, return_tensors="pt")
|
18 |
out = model.generate(**inputs)
|
19 |
short_answer = processor.decode(out[0], skip_special_tokens=True)
|
20 |
|
21 |
+
context = "\n".join([f"Q: {q}\nA: {a}" for q, a in history]) if history else "No previous context."
|
22 |
+
|
23 |
full_prompt = f"""Context of previous conversation:
|
24 |
{context}
|
25 |
|
|
|
34 |
|
35 |
data = {
|
36 |
"model": "llama3-8b-8192",
|
37 |
+
"messages": [
|
38 |
+
{"role": "system", "content": "You are a helpful assistant that answers questions about images based on the provided context and BLIP model's initial analysis."},
|
39 |
+
{"role": "user", "content": full_prompt}
|
40 |
+
]
|
41 |
}
|
42 |
|
43 |
response = requests.post(groq_api_url, headers=headers, json=data)
|
44 |
|
45 |
if response.status_code == 200:
|
46 |
detailed_answer = response.json()['choices'][0]['message']['content'].strip()
|
47 |
+
new_history = history + [(question, detailed_answer)]
|
48 |
+
return new_history, new_history
|
49 |
else:
|
50 |
error_msg = f"Error {response.status_code}: {response.text}"
|
51 |
+
return history + [(question, error_msg)], history + [(question, error_msg)]
|
|
|
52 |
|
53 |
except Exception as e:
|
54 |
error_msg = f"An error occurred: {str(e)}"
|
55 |
+
return history + [(question, error_msg)], history + [(question, error_msg)]
|
|
|
56 |
|
57 |
def clear_history():
|
58 |
return [], []
|
59 |
|
60 |
+
def init_history():
|
61 |
+
return [], []
|
62 |
+
|
63 |
with gr.Blocks() as demo:
|
64 |
gr.Markdown("# Interactive Image Chatbot")
|
65 |
+
gr.Markdown("Upload an image and ask questions about it. The chatbot will maintain context of the conversation.")
|
66 |
|
67 |
with gr.Row():
|
68 |
image_input = gr.Image(type="pil")
|
|
|
70 |
with gr.Row():
|
71 |
with gr.Column():
|
72 |
chatbot = gr.Chatbot()
|
73 |
+
question = gr.Textbox(label="Ask a question about the image", placeholder="Type your question here...")
|
74 |
+
with gr.Row():
|
75 |
+
clear = gr.Button("Clear Conversation")
|
76 |
+
new_image = gr.Button("New Image")
|
77 |
|
78 |
state = gr.State([])
|
79 |
|
80 |
+
# Handle question submission
|
81 |
question.submit(
|
82 |
qna,
|
83 |
inputs=[image_input, question, state],
|
84 |
outputs=[chatbot, state]
|
85 |
)
|
86 |
|
87 |
+
# Handle image upload
|
88 |
+
image_input.change(
|
89 |
+
init_history,
|
90 |
+
outputs=[chatbot, state]
|
91 |
+
)
|
92 |
+
|
93 |
+
# Clear conversation
|
94 |
clear.click(
|
95 |
clear_history,
|
96 |
outputs=[chatbot, state]
|
97 |
)
|
98 |
+
|
99 |
+
# New image button
|
100 |
+
new_image.click(
|
101 |
+
clear_history,
|
102 |
+
outputs=[chatbot, state]
|
103 |
+
)
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
demo.launch()
|