rubenml commited on
Commit
11fcc85
·
verified ·
1 Parent(s): f19ae86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -8,21 +8,26 @@ from transformers import pipeline
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  # --- Basic Agent Definition ---
11
- class BasicAgent:
12
  def __init__(self):
13
- print("Loading QA pipeline...")
14
  self.qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
15
 
16
  def __call__(self, question: str, context: str = None) -> str:
17
- try:
18
- if context is None:
19
- context = question
20
- result = self.qa_pipeline(question=question, context=context)
21
- answer = result["answer"]
22
- except Exception as e:
23
- print(f"Error during QA: {e}")
24
- answer = "Error processing question."
25
- return answer
 
 
 
 
 
26
 
27
  def run_and_submit_all(profile: gr.OAuthProfile | None):
28
  space_id = os.getenv("SPACE_ID")
 
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  # --- Basic Agent Definition ---
11
+ class GeneralAgent:
12
  def __init__(self):
13
+ print("Initializing general QA agent...")
14
  self.qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
15
 
16
  def __call__(self, question: str, context: str = None) -> str:
17
+ """
18
+ Process the question and return an answer based on the given context.
19
+ Uses a prompt template to provide a clear structure for the answer.
20
+ """
21
+ prompt = """
22
+ You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
23
+ """
24
+ question_with_prompt = f"{prompt}\nQuestion: {question}\nContext: {context if context else 'No context provided.'}\n"
25
+
26
+ result = self.qa_pipeline(question=question_with_prompt, context=context)
27
+ answer = result["answer"]
28
+
29
+ # This is where you can modify the final format of the answer if needed.
30
+ return f"FINAL ANSWER: {answer}"
31
 
32
  def run_and_submit_all(profile: gr.OAuthProfile | None):
33
  space_id = os.getenv("SPACE_ID")