Manav0603 commited on
Commit
2274c62
·
verified ·
1 Parent(s): 38f0a7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -7,7 +7,8 @@ from main import process_pdf, query_pdf, qa
7
  # REPLACE "your_actual_token_here" with your real HuggingFace token
8
  os.environ["HUGGINGFACE_TOKEN"] = "your_actual_token_here"
9
 
10
- HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
 
11
  HF_API_TOKEN = os.getenv("token2")
12
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
13
 
@@ -16,24 +17,27 @@ def query_hf_model(prompt):
16
  data = {
17
  "inputs": prompt,
18
  "parameters": {
19
- "max_new_tokens": 512,
20
- "temperature": 0.1,
21
- "top_p": 0.95,
22
  "return_full_text": False
23
  }
24
  }
25
- response = requests.post(HF_API_URL, headers=headers, json=data)
26
- if response.status_code == 200:
27
- result = response.json()
28
- if isinstance(result, list) and len(result) > 0:
29
- if "generated_text" in result[0]:
30
- return result[0]["generated_text"]
 
 
 
 
31
  else:
32
- return str(result[0])
33
  else:
34
- return str(result)
35
- else:
36
- return f"HF API Error: {response.status_code} - {response.text}"
37
 
38
  # ---------------- PDF Processing ----------------
39
  status_message = "Upload a PDF to initialize QA system."
@@ -58,12 +62,11 @@ def answer_question(question):
58
  # Try local QA first (using your Mistral model)
59
  local_answer = query_pdf(question)
60
 
61
- # Also try HF API as backup
62
  try:
63
- hf_prompt = f"[INST] {question} [/INST]"
64
- hf_answer = query_hf_model(hf_prompt)
65
 
66
- return f"**Local Mistral Answer:** {local_answer}\n\n**HF API Answer:** {hf_answer}"
67
  except:
68
  # If HF API fails, just return local answer
69
  return f"**Answer:** {local_answer}"
 
7
  # REPLACE "your_actual_token_here" with your real HuggingFace token
8
  os.environ["HUGGINGFACE_TOKEN"] = "your_actual_token_here"
9
 
10
+ # Use a working model URL for HF Inference API
11
+ HF_API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
12
  HF_API_TOKEN = os.getenv("token2")
13
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
14
 
 
17
  data = {
18
  "inputs": prompt,
19
  "parameters": {
20
+ "max_length": 100,
21
+ "temperature": 0.7,
 
22
  "return_full_text": False
23
  }
24
  }
25
+
26
+ try:
27
+ response = requests.post(HF_API_URL, headers=headers, json=data)
28
+ if response.status_code == 200:
29
+ result = response.json()
30
+ if isinstance(result, list) and len(result) > 0:
31
+ if "generated_text" in result[0]:
32
+ return result[0]["generated_text"]
33
+ else:
34
+ return str(result[0])
35
  else:
36
+ return str(result)
37
  else:
38
+ return f"HF API Error: {response.status_code} - {response.text}"
39
+ except Exception as e:
40
+ return f"API request failed: {str(e)}"
41
 
42
  # ---------------- PDF Processing ----------------
43
  status_message = "Upload a PDF to initialize QA system."
 
62
  # Try local QA first (using your Mistral model)
63
  local_answer = query_pdf(question)
64
 
65
+ # Also try HF API as backup (simpler prompt for DialoGPT)
66
  try:
67
+ hf_answer = query_hf_model(question)
 
68
 
69
+ return f"**Local Answer:** {local_answer}\n\n**HF API Answer:** {hf_answer}"
70
  except:
71
  # If HF API fails, just return local answer
72
  return f"**Answer:** {local_answer}"