wt002 commited on
Commit
624be4a
·
verified ·
1 Parent(s): f75bfd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -80
app.py CHANGED
@@ -3,13 +3,13 @@ from dotenv import load_dotenv
3
  import gradio as gr
4
  import requests
5
 
6
- from typing import List, Dict, Union
7
  import requests
8
- #import wikipediaapi
9
- #import google.generativeai as genai
10
  from typing import List, Dict, Union
11
- import requests
12
  import pandas as pd
 
 
 
13
 
14
  load_dotenv()
15
 
@@ -17,27 +17,22 @@ load_dotenv()
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
- # Configure Gemini
21
- #genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
22
 
23
  # --- Basic Agent Definition ---
24
  class BasicAgent:
25
- def __init__(self, model="google/gemma-7b"):
26
  self.api_url = f"https://api-inference.huggingface.co/models/{model}"
27
  self.headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
 
 
 
 
 
 
 
 
28
  print("BasicAgent initialized.")
29
-
30
- # Initialize other required components
31
- self.searx_url = "https://searx.space/search" # Set your SearxNG instance URL
32
- #self.wiki = wikipediaapi.Wikipedia('en') # Requires wikipedia-api package
33
-
34
- #genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
35
- #self.model = genai.GenerativeModel(model)
36
- #usage
37
- #agent = HuggingFaceAgent("google/gemma-7b") # Same architecture as Gemini
38
- #print(agent.generate("Explain quantum computing"))
39
-
40
-
41
  def __call__(self, question: str) -> str:
42
  print(f"Agent received question (first 50 chars): {question[:50]}...")
43
  fixed_answer = self.generate_response(question)
@@ -45,19 +40,21 @@ class BasicAgent:
45
  return fixed_answer
46
 
47
 
48
- # to check
49
  def generate_response(self, prompt: str) -> str:
50
- """Get response from Gema"""
51
  try:
52
- response = self.model.generate_content(prompt)
53
- return response.text
 
 
 
 
 
54
  except Exception as e:
55
  return f"Error generating response: {str(e)}"
56
 
57
-
58
-
59
  def web_search(self, query: str) -> List[Dict]:
60
- """Use SearxNG meta-search engine"""
61
  params = {
62
  "q": query,
63
  "format": "json",
@@ -70,13 +67,13 @@ class BasicAgent:
70
  except requests.RequestException:
71
  return []
72
 
73
- #def wikipedia_search(self, query: str) -> str:
74
- # """Get Wikipedia summary"""
75
- # page = self.wiki.page(query)
76
- # return page.summary if page.exists() else "No Wikipedia page found"
77
 
78
  def process_document(self, file_path: str) -> str:
79
- """Handle PDF, Word, CSV, Excel files"""
80
  if not os.path.exists(file_path):
81
  return "File not found"
82
 
@@ -84,9 +81,12 @@ class BasicAgent:
84
 
85
  try:
86
  if ext == '.pdf':
87
- return self._process_pdf(file_path)
 
 
88
  elif ext in ('.doc', '.docx'):
89
- return self._process_word(file_path)
 
90
  elif ext == '.csv':
91
  return pd.read_csv(file_path).to_string()
92
  elif ext in ('.xls', '.xlsx'):
@@ -96,54 +96,44 @@ class BasicAgent:
96
  except Exception as e:
97
  return f"Error processing document: {str(e)}"
98
 
99
- def _process_pdf(self, file_path: str) -> str:
100
- """Process PDF using Gemini's vision capability"""
101
- try:
102
- # For Gemini 1.5 or later which supports file uploads
103
- with open(file_path, "rb") as f:
104
- # file = genai.upload_file(f)
105
- response = self.model.generate_content(
106
- ["Extract and summarize the key points from this document:", file]
107
- )
108
- return response.text
109
- except:
110
- # Fallback for older Gemini versions
111
- try:
112
- import PyPDF2
113
- with open(file_path, 'rb') as f:
114
- reader = PyPDF2.PdfReader(f)
115
- return "\n".join([page.extract_text() for page in reader.pages])
116
- except ImportError:
117
- return "PDF processing requires PyPDF2 (pip install PyPDF2)"
118
-
119
- def _process_word(self, file_path: str) -> str:
120
- """Process Word documents"""
121
- try:
122
- from docx import Document
123
- doc = Document(file_path)
124
- return "\n".join([para.text for para in doc.paragraphs])
125
- except ImportError:
126
- return "Word processing requires python-docx (pip install python-docx)"
127
-
128
- def process_request(self, request: Union[str, Dict]) -> str:
129
- """
130
- Handle different request types:
131
- - Direct text queries
132
- - File processing requests
133
- - Complex multi-step requests
134
- """
135
- if isinstance(request, dict):
136
- if 'steps' in request:
137
- results = []
138
- for step in request['steps']:
139
- if step['type'] == 'search':
140
- results.append(self.web_search(step['query']))
141
- elif step['type'] == 'process':
142
- results.append(self.process_document(step['file']))
143
- return self.generate_response(f"Process these results: {results}")
144
- return "Unsupported request format"
145
 
146
- return self.generate_response(request)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  def run_and_submit_all( profile: gr.OAuthProfile | None):
149
  """
 
3
  import gradio as gr
4
  import requests
5
 
6
+ import os
7
  import requests
 
 
8
  from typing import List, Dict, Union
 
9
  import pandas as pd
10
+ import wikipediaapi
11
+ import PyPDF2
12
+ from docx import Document
13
 
14
  load_dotenv()
15
 
 
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
 
 
20
 
21
  # --- Basic Agent Definition ---
22
  class BasicAgent:
23
+ def __init__(self, model="gemini-2.0-flash-lite"):
24
  self.api_url = f"https://api-inference.huggingface.co/models/{model}"
25
  self.headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
26
+ # Wikipedia setup (with proper User-Agent)
27
+ self.wiki = wikipediaapi.Wikipedia(
28
+ language='en',
29
+ user_agent='SearchAgent/1.0 ([email protected])' # CHANGE THIS!
30
+ )
31
+
32
+ # SearxNG meta-search (replace with your instance)
33
+ self.searx_url = "https://searx.space/search" # CHANGE THIS!
34
  print("BasicAgent initialized.")
35
+
 
 
 
 
 
 
 
 
 
 
 
36
  def __call__(self, question: str) -> str:
37
  print(f"Agent received question (first 50 chars): {question[:50]}...")
38
  fixed_answer = self.generate_response(question)
 
40
  return fixed_answer
41
 
42
 
 
43
  def generate_response(self, prompt: str) -> str:
44
+ """Get response from HuggingFace model"""
45
  try:
46
+ response = requests.post(
47
+ self.api_url,
48
+ headers=self.headers,
49
+ json={"inputs": prompt}
50
+ )
51
+ response.raise_for_status()
52
+ return response.json()[0]['generated_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
+ """Search using SearxNG (meta-search engine)"""
58
  params = {
59
  "q": query,
60
  "format": "json",
 
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
+ """Extract text from PDF, Word, CSV, Excel"""
77
  if not os.path.exists(file_path):
78
  return "File not found"
79
 
 
81
 
82
  try:
83
  if ext == '.pdf':
84
+ with open(file_path, 'rb') as f:
85
+ reader = PyPDF2.PdfReader(f)
86
+ return "\n".join([page.extract_text() for page in reader.pages])
87
  elif ext in ('.doc', '.docx'):
88
+ doc = Document(file_path)
89
+ return "\n".join([para.text for para in doc.paragraphs])
90
  elif ext == '.csv':
91
  return pd.read_csv(file_path).to_string()
92
  elif ext in ('.xls', '.xlsx'):
 
96
  except Exception as e:
97
  return f"Error processing document: {str(e)}"
98
 
99
+ def __call__(self, query: str) -> str:
100
+ """Handle queries (text, search, or file processing)"""
101
+ print(f"Processing query: {query[:50]}...")
102
+
103
+ # If it's a file path, process it
104
+ if os.path.exists(query):
105
+ return self.process_document(query)
106
+
107
+ # If it's a Wikipedia-style query (e.g., "wikipedia:Python")
108
+ if query.lower().startswith("wikipedia:"):
109
+ topic = query.split(":")[1].strip()
110
+ return self.wikipedia_search(topic)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ # If it's a web search (e.g., "search:best LLMs 2024")
113
+ if query.lower().startswith("search:"):
114
+ search_query = query.split(":")[1].strip()
115
+ results = self.web_search(search_query)
116
+ return "\n".join([f"{r['title']}: {r['url']}" for r in results])
117
+
118
+ # Default: Use HuggingFace for text generation
119
+ return self.generate_response(query)
120
+
121
+ # Example Usage
122
+ if __name__ == "__main__":
123
+ agent = BasicAgent()
124
+
125
+ # Test Wikipedia search
126
+ print(agent("wikipedia:Python"))
127
+
128
+ # Test web search (requires SearxNG instance)
129
+ # print(agent("search:best programming languages 2024"))
130
+
131
+ # Test text generation
132
+ print(agent("Explain quantum computing in simple terms"))
133
+
134
+ # Test file processing (example: PDF)
135
+ # print(agent("/path/to/document.pdf"))
136
+
137
 
138
  def run_and_submit_all( profile: gr.OAuthProfile | None):
139
  """