Update app.py
Browse files
app.py
CHANGED
@@ -16,15 +16,18 @@ login(token=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
# --- Tool 1: Wikipedia Search Tool ---
|
|
|
|
|
19 |
class WikipediaTool(Tool):
|
20 |
name = "wikipedia_search"
|
21 |
description = "Search Wikipedia for a given topic and return a concise summary."
|
|
|
22 |
parameters = {
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
}
|
29 |
},
|
30 |
"required": ["query"]
|
@@ -35,7 +38,7 @@ class WikipediaTool(Tool):
|
|
35 |
def forward(self, query: str) -> str:
|
36 |
try:
|
37 |
page = wikipedia.page(query)
|
38 |
-
return page.summary[:1000]
|
39 |
except wikipedia.exceptions.DisambiguationError as e:
|
40 |
return f"Disambiguation: {', '.join(e.options[:5])}"
|
41 |
except wikipedia.exceptions.PageError:
|
@@ -44,16 +47,23 @@ class WikipediaTool(Tool):
|
|
44 |
return f"Error: {str(e)}"
|
45 |
|
46 |
|
|
|
47 |
# --- Tool 2: Audio Transcription Tool (uses whisper base) ---
|
|
|
|
|
|
|
|
|
|
|
48 |
class AudioTranscriptionTool(Tool):
|
49 |
name = "audio_transcriber"
|
50 |
description = "Transcribes an uploaded audio file to text using Whisper."
|
|
|
51 |
parameters = {
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
}
|
58 |
},
|
59 |
"required": ["audio"]
|
@@ -61,30 +71,39 @@ class AudioTranscriptionTool(Tool):
|
|
61 |
|
62 |
output_type = "string"
|
63 |
|
64 |
-
def forward(self, audio:
|
65 |
try:
|
|
|
66 |
model = whisper.load_model("base")
|
67 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
68 |
-
tmp_file.write(audio)
|
69 |
-
tmp_file_path = tmp_file.name
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
73 |
return result["text"]
|
74 |
except Exception as e:
|
75 |
return f"Transcription failed: {str(e)}"
|
76 |
|
77 |
|
|
|
78 |
# --- Tool 3: Excel Reader Tool ---
|
|
|
|
|
|
|
|
|
|
|
79 |
class ExcelReaderTool(Tool):
|
80 |
name = "excel_reader"
|
81 |
-
description = "Reads uploaded Excel file and summarizes sheet data."
|
|
|
82 |
parameters = {
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
}
|
89 |
},
|
90 |
"required": ["excel"]
|
@@ -92,18 +111,19 @@ class ExcelReaderTool(Tool):
|
|
92 |
|
93 |
output_type = "string"
|
94 |
|
95 |
-
def forward(self, excel:
|
96 |
try:
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
os.remove(
|
104 |
-
return f"Summary
|
105 |
except Exception as e:
|
106 |
return f"Failed to read Excel file: {str(e)}"
|
|
|
107 |
|
108 |
excel_tool = ExcelReaderTool()
|
109 |
wiki_tool = WikipediaTool()
|
|
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
# --- Tool 1: Wikipedia Search Tool ---
|
19 |
+
import wikipedia
|
20 |
+
|
21 |
class WikipediaTool(Tool):
|
22 |
name = "wikipedia_search"
|
23 |
description = "Search Wikipedia for a given topic and return a concise summary."
|
24 |
+
|
25 |
parameters = {
|
26 |
+
"type": "object",
|
27 |
+
"properties": {
|
28 |
+
"query": {
|
29 |
+
"type": "string",
|
30 |
+
"description": "The topic to search on Wikipedia"
|
31 |
}
|
32 |
},
|
33 |
"required": ["query"]
|
|
|
38 |
def forward(self, query: str) -> str:
|
39 |
try:
|
40 |
page = wikipedia.page(query)
|
41 |
+
return page.summary[:1000]
|
42 |
except wikipedia.exceptions.DisambiguationError as e:
|
43 |
return f"Disambiguation: {', '.join(e.options[:5])}"
|
44 |
except wikipedia.exceptions.PageError:
|
|
|
47 |
return f"Error: {str(e)}"
|
48 |
|
49 |
|
50 |
+
|
51 |
# --- Tool 2: Audio Transcription Tool (uses whisper base) ---
|
52 |
+
import base64
|
53 |
+
import whisper
|
54 |
+
import tempfile
|
55 |
+
import os
|
56 |
+
|
57 |
class AudioTranscriptionTool(Tool):
|
58 |
name = "audio_transcriber"
|
59 |
description = "Transcribes an uploaded audio file to text using Whisper."
|
60 |
+
|
61 |
parameters = {
|
62 |
+
"type": "object",
|
63 |
+
"properties": {
|
64 |
+
"audio": {
|
65 |
+
"type": "string",
|
66 |
+
"description": "The audio file in base64-encoded format"
|
67 |
}
|
68 |
},
|
69 |
"required": ["audio"]
|
|
|
71 |
|
72 |
output_type = "string"
|
73 |
|
74 |
+
def forward(self, audio: str) -> str:
|
75 |
try:
|
76 |
+
decoded_audio = base64.b64decode(audio)
|
77 |
model = whisper.load_model("base")
|
|
|
|
|
|
|
78 |
|
79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
|
80 |
+
tmp.write(decoded_audio)
|
81 |
+
tmp_path = tmp.name
|
82 |
+
|
83 |
+
result = model.transcribe(tmp_path)
|
84 |
+
os.remove(tmp_path)
|
85 |
return result["text"]
|
86 |
except Exception as e:
|
87 |
return f"Transcription failed: {str(e)}"
|
88 |
|
89 |
|
90 |
+
|
91 |
# --- Tool 3: Excel Reader Tool ---
|
92 |
+
import base64
|
93 |
+
import tempfile
|
94 |
+
import pandas as pd
|
95 |
+
import os
|
96 |
+
|
97 |
class ExcelReaderTool(Tool):
|
98 |
name = "excel_reader"
|
99 |
+
description = "Reads uploaded Excel file (.xlsx) and summarizes the sheet data."
|
100 |
+
|
101 |
parameters = {
|
102 |
+
"type": "object",
|
103 |
+
"properties": {
|
104 |
+
"excel": {
|
105 |
+
"type": "string",
|
106 |
+
"description": "The Excel file in base64-encoded format"
|
107 |
}
|
108 |
},
|
109 |
"required": ["excel"]
|
|
|
111 |
|
112 |
output_type = "string"
|
113 |
|
114 |
+
def forward(self, excel: str) -> str:
|
115 |
try:
|
116 |
+
decoded_excel = base64.b64decode(excel)
|
117 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
118 |
+
tmp.write(decoded_excel)
|
119 |
+
tmp_path = tmp.name
|
120 |
+
|
121 |
+
df = pd.read_excel(tmp_path)
|
122 |
+
os.remove(tmp_path)
|
123 |
+
return f"Summary:\n{df.describe(include='all').to_string()}"
|
124 |
except Exception as e:
|
125 |
return f"Failed to read Excel file: {str(e)}"
|
126 |
+
|
127 |
|
128 |
excel_tool = ExcelReaderTool()
|
129 |
wiki_tool = WikipediaTool()
|