dschandra commited on
Commit
31d430e
·
verified ·
1 Parent(s): f277f15

Update knowledge_base.py

Browse files
Files changed (1) hide show
  1. knowledge_base.py +28 -13
knowledge_base.py CHANGED
@@ -1,18 +1,33 @@
1
  import requests
 
 
 
 
 
 
 
 
2
 
3
  def get_answer_from_knowledge_base(question):
4
  """
5
- This function queries a knowledge base or a public API for the answer.
6
- You can replace this with more complex logic or a dynamic database query.
7
  """
8
- # Example using a mock API for current affairs and general knowledge.
9
- # Replace with real-time news or current affairs API as needed.
10
-
11
- api_url = f"https://api.duckduckgo.com/?q={question}&format=json"
12
- response = requests.get(api_url)
13
- data = response.json()
14
-
15
- if 'AbstractText' in data and data['AbstractText']:
16
- return data['AbstractText']
17
- else:
18
- return None
 
 
 
 
 
 
 
 
1
  import requests
2
+ from transformers import pipeline
3
+
4
+ # Load the model and tokenizer using Hugging Face's pipeline
5
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
6
+
7
+ def load_qa_model():
8
+ # Initialize any model settings or variables, if needed
9
+ return qa_pipeline
10
 
11
  def get_answer_from_knowledge_base(question):
12
  """
13
+ This function uses a Hugging Face model (DistilBERT) for question answering
14
+ from a given context.
15
  """
16
+ try:
17
+ # Example: Using a static context for the model to search for answers
18
+ context = """
19
+ Andhra Pradesh (AP) is a state in southern India. The current Chief Minister of Andhra Pradesh is Y. S. Jagan Mohan Reddy.
20
+ He assumed office in May 2019. Jagan Mohan Reddy is from the Yuvajana Sramika Rythu Congress Party (YSRCP).
21
+ """
22
+
23
+ # Using the question-answering pipeline from Hugging Face
24
+ result = qa_pipeline(question=question, context=context)
25
+
26
+ # Extract and return the answer from the result
27
+ if result['score'] > 0.1: # Only return answer if the model is confident
28
+ return result['answer']
29
+ else:
30
+ return None
31
+ except Exception as e:
32
+ print(f"Error occurred: {e}")
33
+ return "Sorry, I couldn't fetch data at the moment. Please try again later."