kpbotla commited on
Commit
e1ccec3
·
verified ·
1 Parent(s): a7414db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -128
app.py CHANGED
@@ -25,7 +25,11 @@ from transformers import BlipProcessor, BlipForConditionalGeneration
25
 
26
  import re
27
  from collections import defaultdict
28
-
 
 
 
 
29
 
30
 
31
  # (Keep Constants as is)
@@ -34,137 +38,98 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
34
 
35
  # --- Basic Agent Definition ---
36
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
37
- # Summarization pipeline (load once)
38
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
39
-
40
-
41
-
42
 
43
-
44
- class SmartAgentV2:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def __init__(self):
46
- self.qa_model = pipeline("text2text-generation", model="google/flan-t5-xl")
47
- self.summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
48
- self.whisper_model = whisper.load_model("base")
49
-
50
- def search_web(self, query):
51
- with DDGS() as ddgs:
52
- results = ddgs.text(query, max_results=3)
53
- for r in results:
54
- if "href" in r:
55
- return r["href"]
56
- return "No results found."
57
-
58
- def summarize_url(self, url):
59
  try:
60
- article = Article(url)
61
- article.download()
62
- article.parse()
63
- text = article.text
64
- if not text.strip():
65
- return "No content found."
66
- summary = self.summarizer(text, max_length=150, min_length=40, do_sample=False)
67
- return summary[0]['summary_text'].strip()
68
  except Exception as e:
69
- logging.error(f"Summarization error: {e}")
70
- return "Error summarizing."
71
-
72
- def generate_citation(self, url):
73
- citation_id = hashlib.md5(url.encode()).hexdigest()[:6]
74
- year = datetime.datetime.now().year
75
- return f"@article{{cite{citation_id}, title={{Generated Citation}}, author={{Unknown}}, journal={{Online}}, year={{ {year} }}, url={{ {url} }} }}"
76
-
77
- def transcribe_audio(self, filepath):
78
- result = self.whisper_model.transcribe(filepath)
79
- return result["text"]
80
-
81
- def extract_ingredients(self, transcript):
82
- ingredients = re.findall(r"(?:\ba|\ban|\bthe)?\s*([a-zA-Z\s]+?)\s*(?:\bof\b|\bcups?\b|\btablespoons?\b|\bteaspoons?\b|\bpinch\b)?", transcript)
83
- ingredients = [i.strip().lower() for i in ingredients if len(i.strip()) > 2]
84
- return ", ".join(sorted(set(ingredients)))
85
-
86
- def extract_page_numbers(self, transcript):
87
- numbers = re.findall(r"\b\d+\b", transcript)
88
- return ", ".join(sorted(set(numbers), key=int))
89
-
90
- def sum_food_sales(self, filepath):
91
- df = pd.read_excel(filepath)
92
- food_df = df[df["Category"].str.lower() == "food"]
93
- total = food_df["Sales"].sum()
94
- return f"${total:.2f}"
95
-
96
- def answer_fact(self, question):
97
- return self.qa_model(question, max_length=100)[0]["generated_text"].strip()
98
-
99
- def reverse_text_puzzle(self, line):
100
- try:
101
- return ''.join(reversed(line.strip()))
102
- except:
103
- return "Could not reverse text."
104
-
105
- def non_commutative_subset(self):
106
- return "a, b, c"
107
-
108
- def true_vegetables(self):
109
- vegetables = [
110
- "broccoli", "celery", "green beans", "lettuce", "sweet potatoes", "zucchini"
111
- ]
112
- return ", ".join(sorted(vegetables))
113
-
114
- def get_wikipedia_answer(self, question):
115
- try:
116
- search_url = self.search_web(question)
117
- response = requests.get(search_url, timeout=10)
118
- soup = BeautifulSoup(response.text, 'html.parser')
119
- paragraphs = soup.find_all('p')
120
- full_text = ' '.join(p.text for p in paragraphs[:5])
121
- answer = self.qa_model(question + "\n" + full_text, max_length=100)[0]['generated_text']
122
- return answer.strip()
123
- except Exception as e:
124
- logging.error(f"Wikipedia fallback failed: {e}")
125
- return "Could not find answer from Wikipedia."
126
-
127
- def __call__(self, question: str, file=None):
128
- q = question.lower().strip()
129
- try:
130
- if any(word in q for word in ["image", "chess", "diagram"]):
131
- return "I'm a text-only agent and cannot interpret images."
132
- if any(word in q for word in ["youtube", "video"]):
133
- return "I'm unable to access or analyze video/audio from YouTube."
134
- if 'etirw ,ecnetnes' in q:
135
- return self.reverse_text_puzzle(question)
136
- if "counter-examples" in q and "commutative" in q:
137
- return self.non_commutative_subset()
138
- if "vegetables" in q and "botany" in q:
139
- return self.true_vegetables()
140
- if file:
141
- if filepath := getattr(file, "name", None):
142
- if filepath.endswith(".mp3"):
143
- transcript = self.transcribe_audio(filepath)
144
- if "ingredient" in q:
145
- return self.extract_ingredients(transcript)
146
- if "page" in q:
147
- return self.extract_page_numbers(transcript)
148
- return transcript
149
- elif filepath.endswith(".xlsx") or filepath.endswith(".xls"):
150
- return self.sum_food_sales(filepath)
151
- if q.startswith("summarize:"):
152
- url = question.split(":", 1)[1].strip()
153
- return self.summarize_url(url)
154
- elif q.startswith("generate citation:") or q.startswith("cite:"):
155
- url = question.split(":", 1)[1].strip()
156
- return self.generate_citation(url)
157
- elif q.startswith("search:"):
158
- query = question.split(":", 1)[1].strip()
159
- return self.search_web(query)
160
- elif "wikipedia" in q:
161
- return self.get_wikipedia_answer(question)
162
- else:
163
- return self.answer_fact(question)
164
- except Exception as e:
165
- logging.error(f"Error: {e}")
166
- return "An error occurred processing the question."
167
-
168
 
169
 
170
 
 
25
 
26
  import re
27
  from collections import defaultdict
28
+ from pytube import YouTube
29
+ import wikipediaapi
30
+ from langchain.agents import initialize_agent, Tool
31
+ from langchain.llms import HuggingFaceHub
32
+ from langchain.tools import PythonREPL
33
 
34
 
35
  # (Keep Constants as is)
 
38
 
39
  # --- Basic Agent Definition ---
40
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
41
 
42
+ # --- Agent Tools ---
43
+ def wikipedia_lookup(query):
44
+ wiki_wiki = wikipediaapi.Wikipedia('en')
45
+ page = wiki_wiki.page(query)
46
+ if not page.exists():
47
+ return f"Wikipedia page for '{query}' not found."
48
+ return page.summary[:1024]
49
+
50
+ wiki_tool = Tool(
51
+ name="WikipediaTool",
52
+ func=wikipedia_lookup,
53
+ description="Use for looking up facts or summaries from English Wikipedia."
54
+ )
55
+
56
+ def get_youtube_transcript(url):
57
+ try:
58
+ yt = YouTube(url)
59
+ caption = yt.captions.get_by_language_code('en')
60
+ return caption.generate_srt_captions()[:2048]
61
+ except Exception as e:
62
+ return f"Failed to retrieve transcript: {str(e)}"
63
+
64
+ youtube_tool = Tool(
65
+ name="YouTubeTranscriptTool",
66
+ func=get_youtube_transcript,
67
+ description="Use to retrieve English captions from a YouTube video URL."
68
+ )
69
+
70
+ def transcribe_audio(file_path):
71
+ model = whisper.load_model("base")
72
+ result = model.transcribe(file_path)
73
+ return result['text'][:2048]
74
+
75
+ audio_tool = Tool(
76
+ name="AudioTranscriber",
77
+ func=transcribe_audio,
78
+ description="Transcribes short English audio files (MP3/WAV)."
79
+ )
80
+
81
+ def extract_food_sales(filepath):
82
+ wb = openpyxl.load_workbook(filepath)
83
+ sheet = wb.active
84
+ total = 0
85
+ for row in sheet.iter_rows(min_row=2, values_only=True):
86
+ item, category, sales = row
87
+ if category.lower() == 'food':
88
+ total += float(sales)
89
+ return f"Total food sales: ${total:.2f}"
90
+
91
+ excel_tool = Tool(
92
+ name="ExcelFoodSales",
93
+ func=extract_food_sales,
94
+ description="Use to calculate total food sales from an Excel file with columns: item, category, sales."
95
+ )
96
+
97
+ def describe_image(image_path):
98
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
99
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
100
+ raw_image = Image.open(image_path).convert('RGB')
101
+ inputs = processor(raw_image, return_tensors="pt")
102
+ out = model.generate(**inputs)
103
+ caption = processor.decode(out[0], skip_special_tokens=True)
104
+ return caption
105
+
106
+ image_tool = Tool(
107
+ name="ImageDescriber",
108
+ func=describe_image,
109
+ description="Use to describe an image (e.g., chessboard layout or other visual input)."
110
+ )
111
+
112
+ repl_tool = PythonREPL()
113
+
114
+ llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature": 0.2, "max_length": 1024})
115
+
116
+ tools = [wiki_tool, youtube_tool, audio_tool, excel_tool, image_tool, repl_tool]
117
+ agent_instance = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
118
+
119
+ # --- Enhanced Agent ---
120
+ class BasicAgent:
121
  def __init__(self):
122
+ print("Advanced GAIA Agent initialized.")
123
+ def __call__(self, question: str) -> str:
124
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
 
 
 
 
 
 
 
 
 
 
125
  try:
126
+ result = agent_instance.run(question)
127
+ print(f"Agent response: {result[:100]}")
128
+ return result
 
 
 
 
 
129
  except Exception as e:
130
+ error_message = f"ERROR: {e}"
131
+ print(error_message)
132
+ return error_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
 
135