786avinash commited on
Commit
b488bd8
·
verified ·
1 Parent(s): 0bd99be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -29
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
- # Set your OpenAI API key
11
- openai.api_key = "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" # Replace with your OpenAI API key
 
12
 
13
- # Function to generate the initial answer with BLIP and expand it with OpenAI API
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 OpenAI API
22
- prompt = f"Question: {question}\nShort Answer: {short_answer}\nProvide a detailed explanation based on this answer"
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Step 3: Send prompt to OpenAI API for a paragraph-length answer
25
- response = openai.Completion.create(
26
- engine="text-davinci-003", # Specify model
27
- prompt=prompt,
28
- max_tokens=200 # Adjust max_tokens as needed for response length
29
- )
30
- detailed_answer = response.choices[0].text.strip()
 
31
 
32
- return detailed_answer
 
33
 
34
- except openai.error.OpenAIError as e:
35
- # Log full error details in the backend
36
- print(f"OpenAI API error: {e}")
37
- # Return a user-friendly error message to the Gradio interface
38
- return "An error occurred while generating the response. Please try again later."
39
 
40
- except Exception as e:
41
- # Log any other exceptions in the backend
42
- print(f"General exception: {e}")
43
- # Return a general error message to the Gradio interface
44
- return "A technical issue occurred. Please try again later."
45
-
46
- # Create Gradio interface
47
- interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
48
- interf.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
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()