pavan-d commited on
Commit
290b210
·
verified ·
1 Parent(s): f3b7ae4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -6,6 +6,8 @@ import pandas as pd
6
 
7
  import torch
8
  from transformers import pipeline
 
 
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
@@ -13,33 +15,48 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
16
  class BasicAgent:
17
  def __init__(self):
18
- print("🔄 Loading Zephyr 7B...")
 
 
19
  self.llm = pipeline(
20
  "text-generation",
21
  model="HuggingFaceH4/zephyr-7b-beta",
22
  tokenizer="HuggingFaceH4/zephyr-7b-beta",
23
- max_new_tokens=200,
24
  temperature=0,
25
  device=0 if torch.cuda.is_available() else -1
26
  )
27
- print("✅ Zephyr 7B Loaded.")
28
 
29
  def __call__(self, question: str) -> str:
30
- print(f"🤖 Agent received question: {question}")
31
 
32
- prompt = f"<|system|>You are a helpful assistant.<|user|>{question}<|assistant|>"
 
 
 
33
 
34
- try:
35
- output = self.llm(prompt)[0]["generated_text"]
36
- # Post-process to remove prompt from output
37
- answer = output.split("<|assistant|>")[-1].strip()
38
- print(f"✅ Answer: {answer}")
39
- return answer
40
- except Exception as e:
41
- print(f" Error generating response: {e}")
42
- return "I don't know"
 
 
 
 
 
 
 
 
 
43
 
44
  def run_and_submit_all( profile: gr.OAuthProfile | None):
45
  """
 
6
 
7
  import torch
8
  from transformers import pipeline
9
+ import wikipediaapi
10
+ import textwrap
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
 
15
 
16
  # --- Basic Agent Definition ---
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
+
19
  class BasicAgent:
20
  def __init__(self):
21
+ print("Loading Wikipedia...")
22
+ self.wiki = wikipediaapi.Wikipedia('en')
23
+ print("Loading Zephyr LLM pipeline...")
24
  self.llm = pipeline(
25
  "text-generation",
26
  model="HuggingFaceH4/zephyr-7b-beta",
27
  tokenizer="HuggingFaceH4/zephyr-7b-beta",
28
+ max_new_tokens=300,
29
  temperature=0,
30
  device=0 if torch.cuda.is_available() else -1
31
  )
32
+ print("Ready.")
33
 
34
  def __call__(self, question: str) -> str:
35
+ print(f"🧠 Question: {question}")
36
 
37
+ # 1. Fetch page content
38
+ page = self.wiki.page("Mercedes Sosa")
39
+ if not page.exists():
40
+ return "Wikipedia page not found."
41
 
42
+ text = page.text
43
+ chunks = textwrap.wrap(text, width=2000) # break into ~2k token-like chunks
44
+
45
+ best_answer = ""
46
+ for chunk in chunks:
47
+ prompt = (
48
+ "<|system|>You are a precise assistant using Wikipedia.</s>\n"
49
+ f"<|user|>{question}\n\nHere is relevant context from Wikipedia:\n{chunk}\n<|assistant|>"
50
+ )
51
+
52
+ result = self.llm(prompt)[0]["generated_text"]
53
+ answer = result.split("<|assistant|>")[-1].strip()
54
+
55
+ if any(char.isdigit() for char in answer): # naive check for answer-like text
56
+ best_answer = answer
57
+ break # stop early if answer found
58
+
59
+ return best_answer or "I don't know"
60
 
61
  def run_and_submit_all( profile: gr.OAuthProfile | None):
62
  """