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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -1,38 +1,36 @@
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
 
 
1
  from transformers import BlipForQuestionAnswering, AutoProcessor
2
  from PIL import Image
3
  import gradio as gr
4
+ import openai # For OpenAI 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
+ # 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
  # Step 1: Get initial short answer from BLIP
16
  inputs = processor(image, question, return_tensors="pt")
17
  out = model.generate(**inputs)
18
  short_answer = processor.decode(out[0], skip_special_tokens=True)
19
 
20
+ # Step 2: Construct prompt for OpenAI API
21
  prompt = f"Question: {question}\nShort Answer: {short_answer}\nProvide a detailed explanation based on this answer."
22
 
23
+ # Step 3: Send prompt to OpenAI API for a paragraph-length answer
24
+ try:
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
+ except Exception as e:
32
+ print(f"Exception occurred: {e}")
33
+ detailed_answer = "Failed to get response from OpenAI API."
 
34
 
35
  return detailed_answer
36