786avinash commited on
Commit
900d27d
·
verified ·
1 Parent(s): 03fdc9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -1,12 +1,41 @@
1
- from transformers import BlipForQuestionAnswering
2
- from transformers import AutoProcessor
3
  from PIL import Image
4
  import gradio as gr
5
- model=BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
6
- processor=AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
7
- def qna(image,question):
8
- inputs=processor(image,question,return_tensors="pt")
9
- out=model.generate(**inputs)
10
- return processor.decode(out[0],skip_special_tokens=True)
11
- interf=gr.Interface(qna,inputs=["image","text"],outputs="text")
12
- interf.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlipForQuestionAnswering, AutoProcessor
 
2
  from PIL import Image
3
  import gradio as gr
4
+ import requests # To call Groq API
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 Groq API details (Replace with actual API endpoint and key)
11
+ GROQ_API_ENDPOINT = "https://api.groq.com/v1/completions"
12
+ GROQ_API_KEY = "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" # Replace with your actual Groq API key
13
+
14
+ # Function to generate the initial answer with BLIP and expand it with Groq API
15
+ def qna(image, question):
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
+ prompt = f"Question: {question}\nShort Answer: {short_answer}\nProvide a detailed explanation based on this answer."
23
+
24
+ # Step 3: Send prompt to Groq API for a paragraph-length answer
25
+ response = requests.post(
26
+ GROQ_API_ENDPOINT,
27
+ headers={"Authorization": f"Bearer {GROQ_API_KEY}"},
28
+ json={"prompt": prompt, "max_tokens": 200} # Adjust max_tokens as needed
29
+ )
30
+
31
+ if response.status_code == 200:
32
+ # Extract generated paragraph from Groq API response
33
+ detailed_answer = response.json().get("choices")[0].get("text")
34
+ else:
35
+ detailed_answer = "Failed to get response from Groq API."
36
+
37
+ return detailed_answer
38
+
39
+ # Create Gradio interface
40
+ interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
41
+ interf.launch()