Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,36 @@
|
|
1 |
|
2 |
-
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
-
|
6 |
-
from typing import List, Dict, Union, Optional
|
7 |
import pandas as pd
|
8 |
-
import
|
9 |
-
import
|
10 |
-
|
11 |
-
import random
|
12 |
-
import re
|
13 |
-
from typing import Optional
|
14 |
-
from datetime import datetime
|
15 |
-
import google.generativeai as genai
|
16 |
|
17 |
-
load_dotenv()
|
18 |
|
19 |
# (Keep Constants as is)
|
20 |
# --- Constants ---
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
22 |
|
23 |
-
|
24 |
# --- Basic Agent Definition ---
|
|
|
|
|
25 |
|
26 |
class BasicAgent:
|
27 |
-
|
28 |
-
|
29 |
-
Multi-modal agent powered by Google Gemini with:
|
30 |
-
- Web search
|
31 |
-
- Wikipedia access
|
32 |
-
- Document processing
|
33 |
-
"""
|
34 |
-
self.model = genai.GenerativeModel(model_name)
|
35 |
-
self.wiki = wikipediaapi.Wikipedia('en')
|
36 |
-
self.searx_url = "https://searx.space/search" # Public Searx instance
|
37 |
-
|
38 |
print("BasicAgent initialized.")
|
|
|
39 |
|
40 |
-
|
41 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
def generate_response(self, prompt: str) -> str:
|
49 |
-
"""Get response from Gemini"""
|
50 |
-
try:
|
51 |
-
response = self.model.generate_content(prompt)
|
52 |
-
return response.text
|
53 |
-
except Exception as e:
|
54 |
-
return f"Error generating response: {str(e)}"
|
55 |
|
56 |
-
def web_search(self, query: str) -> List[Dict]:
|
57 |
-
"""Use SearxNG meta-search engine"""
|
58 |
-
params = {
|
59 |
-
"q": query,
|
60 |
-
"format": "json",
|
61 |
-
"engines": "google,bing,duckduckgo"
|
62 |
-
}
|
63 |
-
try:
|
64 |
-
response = requests.get(self.searx_url, params=params)
|
65 |
-
response.raise_for_status()
|
66 |
-
return response.json().get("results", [])
|
67 |
-
except requests.RequestException:
|
68 |
-
return []
|
69 |
-
|
70 |
-
def wikipedia_search(self, query: str) -> str:
|
71 |
-
"""Get Wikipedia summary"""
|
72 |
-
page = self.wiki.page(query)
|
73 |
-
return page.summary if page.exists() else "No Wikipedia page found"
|
74 |
-
|
75 |
-
def process_document(self, file_path: str) -> str:
|
76 |
-
"""Handle PDF, Word, CSV, Excel files"""
|
77 |
-
if not os.path.exists(file_path):
|
78 |
-
return "File not found"
|
79 |
-
|
80 |
-
ext = os.path.splitext(file_path)[1].lower()
|
81 |
-
|
82 |
-
try:
|
83 |
-
if ext == '.pdf':
|
84 |
-
return self._process_pdf(file_path)
|
85 |
-
elif ext in ('.doc', '.docx'):
|
86 |
-
return self._process_word(file_path)
|
87 |
-
elif ext == '.csv':
|
88 |
-
return pd.read_csv(file_path).to_string()
|
89 |
-
elif ext in ('.xls', '.xlsx'):
|
90 |
-
return pd.read_excel(file_path).to_string()
|
91 |
-
else:
|
92 |
-
return "Unsupported file format"
|
93 |
-
except Exception as e:
|
94 |
-
return f"Error processing document: {str(e)}"
|
95 |
-
|
96 |
-
def _process_pdf(self, file_path: str) -> str:
|
97 |
-
"""Process PDF using Gemini's vision capability"""
|
98 |
-
try:
|
99 |
-
# For Gemini 1.5 or later which supports file uploads
|
100 |
-
with open(file_path, "rb") as f:
|
101 |
-
file = genai.upload_file(f)
|
102 |
-
response = self.model.generate_content(
|
103 |
-
["Extract and summarize the key points from this document:", file]
|
104 |
-
)
|
105 |
-
return response.text
|
106 |
-
except:
|
107 |
-
# Fallback for older Gemini versions
|
108 |
-
try:
|
109 |
-
import PyPDF2
|
110 |
-
with open(file_path, 'rb') as f:
|
111 |
-
reader = PyPDF2.PdfReader(f)
|
112 |
-
return "\n".join([page.extract_text() for page in reader.pages])
|
113 |
-
except ImportError:
|
114 |
-
return "PDF processing requires PyPDF2 (pip install PyPDF2)"
|
115 |
-
|
116 |
-
def _process_word(self, file_path: str) -> str:
|
117 |
-
"""Process Word documents"""
|
118 |
-
try:
|
119 |
-
from docx import Document
|
120 |
-
doc = Document(file_path)
|
121 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
122 |
-
except ImportError:
|
123 |
-
return "Word processing requires python-docx (pip install python-docx)"
|
124 |
-
|
125 |
-
def process_request(self, request: Union[str, Dict]) -> str:
|
126 |
-
"""
|
127 |
-
Handle different request types:
|
128 |
-
- Direct text queries
|
129 |
-
- File processing requests
|
130 |
-
- Complex multi-step requests
|
131 |
-
"""
|
132 |
-
if isinstance(request, dict):
|
133 |
-
if 'steps' in request:
|
134 |
-
results = []
|
135 |
-
for step in request['steps']:
|
136 |
-
if step['type'] == 'search':
|
137 |
-
results.append(self.web_search(step['query']))
|
138 |
-
elif step['type'] == 'process':
|
139 |
-
results.append(self.process_document(step['file']))
|
140 |
-
return self.generate_response(f"Process these results: {results}")
|
141 |
-
return "Unsupported request format"
|
142 |
-
|
143 |
-
return self.generate_response(request)
|
144 |
|
145 |
|
146 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
1 |
|
2 |
+
import os
|
3 |
+
import inspect
|
4 |
import gradio as gr
|
5 |
import requests
|
|
|
|
|
6 |
import pandas as pd
|
7 |
+
from langchain_core.messages import HumanMessage
|
8 |
+
from agent import build_graph
|
9 |
+
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
11 |
|
12 |
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
|
|
16 |
# --- Basic Agent Definition ---
|
17 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
+
|
19 |
|
20 |
class BasicAgent:
|
21 |
+
"""A langgraph agent."""
|
22 |
+
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
print("BasicAgent initialized.")
|
24 |
+
self.graph = build_graph()
|
25 |
|
26 |
+
def __call__(self, question: str) -> str:
|
27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
28 |
+
# Wrap the question in a HumanMessage from langchain_core
|
29 |
+
messages = [HumanMessage(content=question)]
|
30 |
+
messages = self.graph.invoke({"messages": messages})
|
31 |
+
answer = messages['messages'][-1].content
|
32 |
+
return answer[14:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|