baha-99 commited on
Commit
f0c781a
·
1 Parent(s): 621129f

fix: Not building on hf problem fixed.4

Browse files
Files changed (2) hide show
  1. app.py +12 -23
  2. bot_telegram.py +18 -19
app.py CHANGED
@@ -1,26 +1,15 @@
1
- from fastapi import FastAPI
2
- import logging
3
-
4
  from bot_telegram import init_bot
5
 
6
- app = FastAPI()
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
- await bot.run()
15
-
16
- @app.on_event("shutdown")
17
- async def shutdown_event():
18
- """Stop the Telegram bot when the FastAPI application stops."""
19
- global bot
20
- if bot:
21
- logging.info("Stopping bot...")
22
- await bot.bot_stop()
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
- # Separate executors for text and Excel
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": hf_authorization,
190
  "accept": "application/json"
191
  }
192
 
193
  json_payload = {"question": user_message}
194
  form_payload = {"question": user_message}
195
 
196
- try:
 
 
 
 
 
 
197
  response = requests.post(
198
  self.ai_url,
199
- headers={**headers, "Content-Type": "application/json"},
200
- json=json_payload
201
  )
202
 
203
- if response.status_code == 422:
204
- response = requests.post(
205
- self.ai_url,
206
- headers={**headers, "Content-Type": "application/x-www-form-urlencoded"},
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
- except Exception as e:
216
- return f"Connection error: {e}"
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"""