Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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("
|
|
|
|
|
|
|
| 19 |
self.llm = pipeline(
|
| 20 |
"text-generation",
|
| 21 |
model="HuggingFaceH4/zephyr-7b-beta",
|
| 22 |
tokenizer="HuggingFaceH4/zephyr-7b-beta",
|
| 23 |
-
max_new_tokens=
|
| 24 |
temperature=0,
|
| 25 |
device=0 if torch.cuda.is_available() else -1
|
| 26 |
)
|
| 27 |
-
print("
|
| 28 |
|
| 29 |
def __call__(self, question: str) -> str:
|
| 30 |
-
print(f"
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
"""
|