Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,75 @@
|
|
|
|
1 |
from transformers import BlipForQuestionAnswering, AutoProcessor
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
-
import openai
|
5 |
|
6 |
# Load the BLIP model and processor
|
7 |
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
8 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
def qna(image, question):
|
15 |
try:
|
16 |
# Step 1: Get initial short answer from BLIP
|
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 |
-
# Step 2: Construct prompt for
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
response
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
#
|
47 |
-
interf = gr.Interface(
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
from transformers import BlipForQuestionAnswering, AutoProcessor
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
# Load the BLIP model and processor
|
7 |
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
8 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
9 |
|
10 |
+
# Define your Groq API key and endpoint
|
11 |
+
groq_api_key = "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" # Replace with your Groq API key
|
12 |
+
groq_api_url = "https://api.groq.com/openai/v1/chat/completions" # Replace with the appropriate Groq endpoint
|
13 |
|
14 |
+
def qna(image, question, context):
|
|
|
15 |
try:
|
16 |
# Step 1: Get initial short answer from BLIP
|
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 |
+
# Step 2: Construct prompt for Groq API
|
22 |
+
full_prompt = f"{context}\nUser: {question}\nBLIP: {short_answer}\nAssistant:"
|
23 |
+
|
24 |
+
# Step 3: Send prompt to Groq API for a detailed answer
|
25 |
+
headers = {
|
26 |
+
"Authorization": f"Bearer {groq_api_key}",
|
27 |
+
"Content-Type": "application/json"
|
28 |
+
}
|
29 |
+
|
30 |
+
data = {
|
31 |
+
"model": "llama3-8b-8192", # Specify the model to use
|
32 |
+
"messages": [{"role": "user", "content": full_prompt}]
|
33 |
+
}
|
34 |
+
|
35 |
+
response = requests.post(groq_api_url, headers=headers, json=data)
|
36 |
|
37 |
+
# Check if the response is successful
|
38 |
+
if response.status_code == 200:
|
39 |
+
detailed_answer = response.json().get('choices', [])[0].get('message', {}).get('content', '').strip()
|
40 |
+
# Update the context with the latest question and answer
|
41 |
+
updated_context = f"{context}\nUser: {question}\nAssistant: {detailed_answer}"
|
42 |
+
return updated_context, updated_context # Return updated context for display
|
43 |
+
else:
|
44 |
+
return f"Error {response.status_code}: {response.text}", context
|
45 |
|
46 |
+
except Exception as e:
|
47 |
+
return f"An error occurred: {str(e)}", context
|
48 |
|
49 |
+
# Create Gradio interface with context management
|
50 |
+
def chatbot_interface(image, question, context=""):
|
51 |
+
# Initialize context if image is uploaded
|
52 |
+
if context == "" and image is not None:
|
53 |
+
context = "" # Reset context when the image is first uploaded
|
54 |
|
55 |
+
# Get the answer from the model
|
56 |
+
answer, updated_context = qna(image, question, context)
|
57 |
+
|
58 |
+
# Return the updated context for display
|
59 |
+
return updated_context
|
60 |
+
|
61 |
+
# Define the Gradio interface
|
62 |
+
interf = gr.Interface(
|
63 |
+
fn=chatbot_interface,
|
64 |
+
inputs=[
|
65 |
+
gr.Image(type="pil", label="Upload Image"),
|
66 |
+
gr.Textbox(label="Ask a question")
|
67 |
+
],
|
68 |
+
outputs="text", # Output the full conversation context
|
69 |
+
title="Interactive Image Chatbot",
|
70 |
+
description="Upload an image and have a conversation about it. Ask multiple questions about the image."
|
71 |
+
)
|
72 |
+
|
73 |
+
# Launch the interface
|
74 |
+
if __name__ == "__main__":
|
75 |
+
interf.launch()
|