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