Spaces:
Paused
Paused
fix: Not building on hf problem fixed.4
Browse files- app.py +12 -23
- bot_telegram.py +18 -19
app.py
CHANGED
|
@@ -1,26 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
import logging
|
| 3 |
-
|
| 4 |
from bot_telegram import init_bot
|
| 5 |
|
| 6 |
-
|
| 7 |
-
bot = None
|
| 8 |
-
|
| 9 |
-
@app.on_event("startup")
|
| 10 |
-
async def startup_event():
|
| 11 |
-
"""Start the Telegram bot when the FastAPI application starts."""
|
| 12 |
-
global bot
|
| 13 |
bot = init_bot()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
@app.get("/")
|
| 25 |
-
def greet_json():
|
| 26 |
-
return {"Hello": "World!"}
|
|
|
|
| 1 |
+
import asyncio
|
|
|
|
|
|
|
| 2 |
from bot_telegram import init_bot
|
| 3 |
|
| 4 |
+
async def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
bot = init_bot()
|
| 6 |
+
try:
|
| 7 |
+
await bot.run()
|
| 8 |
+
# Keep the bot running
|
| 9 |
+
while True:
|
| 10 |
+
await asyncio.sleep(1)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Bot error: {e}")
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
asyncio.run(main())
|
|
|
|
|
|
|
|
|
bot_telegram.py
CHANGED
|
@@ -45,9 +45,10 @@ class TelegramBot:
|
|
| 45 |
self.ai_url = f"{self.base_url}/api/v1/questions/text"
|
| 46 |
self.excel_url = f"{self.base_url}/api/v1/questions/excel"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
self.text_executor = ThreadPoolExecutor(max_workers=25, thread_name_prefix="text_worker")
|
| 50 |
self.excel_executor = ThreadPoolExecutor(max_workers=20, thread_name_prefix="excel_worker")
|
|
|
|
| 51 |
|
| 52 |
# Track active processes
|
| 53 |
self.active_requests = {}
|
|
@@ -184,36 +185,34 @@ class TelegramBot:
|
|
| 184 |
def _make_api_request(self, user_message):
|
| 185 |
"""Make API request"""
|
| 186 |
try:
|
| 187 |
-
hf_authorization = "Bearer " + HF_TOKEN
|
| 188 |
headers = {
|
| 189 |
-
"Authorization":
|
| 190 |
"accept": "application/json"
|
| 191 |
}
|
| 192 |
|
| 193 |
json_payload = {"question": user_message}
|
| 194 |
form_payload = {"question": user_message}
|
| 195 |
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
response = requests.post(
|
| 198 |
self.ai_url,
|
| 199 |
-
headers={**headers, "Content-Type": "application/
|
| 200 |
-
|
| 201 |
)
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
data=form_payload
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
if response.status_code == 200:
|
| 211 |
-
return response.json().get("answer", "I didn't understand that.")
|
| 212 |
-
else:
|
| 213 |
-
return f"Error: {response.status_code}"
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
|
| 218 |
async def handle_excel(self, update: Update, context: CallbackContext):
|
| 219 |
"""Handle Excel files concurrently"""
|
|
|
|
| 45 |
self.ai_url = f"{self.base_url}/api/v1/questions/text"
|
| 46 |
self.excel_url = f"{self.base_url}/api/v1/questions/excel"
|
| 47 |
|
| 48 |
+
# Executors (keep only one instance of each)
|
| 49 |
self.text_executor = ThreadPoolExecutor(max_workers=25, thread_name_prefix="text_worker")
|
| 50 |
self.excel_executor = ThreadPoolExecutor(max_workers=20, thread_name_prefix="excel_worker")
|
| 51 |
+
self.excel_semaphore = asyncio.Semaphore(10)
|
| 52 |
|
| 53 |
# Track active processes
|
| 54 |
self.active_requests = {}
|
|
|
|
| 185 |
def _make_api_request(self, user_message):
|
| 186 |
"""Make API request"""
|
| 187 |
try:
|
|
|
|
| 188 |
headers = {
|
| 189 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 190 |
"accept": "application/json"
|
| 191 |
}
|
| 192 |
|
| 193 |
json_payload = {"question": user_message}
|
| 194 |
form_payload = {"question": user_message}
|
| 195 |
|
| 196 |
+
response = requests.post(
|
| 197 |
+
self.ai_url,
|
| 198 |
+
headers={**headers, "Content-Type": "application/json"},
|
| 199 |
+
json=json_payload
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
if response.status_code == 422:
|
| 203 |
response = requests.post(
|
| 204 |
self.ai_url,
|
| 205 |
+
headers={**headers, "Content-Type": "application/x-www-form-urlencoded"},
|
| 206 |
+
data=form_payload
|
| 207 |
)
|
| 208 |
|
| 209 |
+
if response.status_code == 200:
|
| 210 |
+
return response.json().get("answer", "I didn't understand that.")
|
| 211 |
+
else:
|
| 212 |
+
return f"Error: {response.status_code}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
+
except Exception as e:
|
| 215 |
+
return f"Connection error: {e}"
|
| 216 |
|
| 217 |
async def handle_excel(self, update: Update, context: CallbackContext):
|
| 218 |
"""Handle Excel files concurrently"""
|