Deploy Bot commited on
Commit
6948eae
Β·
1 Parent(s): c4cbaf0

Remove Dockerfile, fix app.py for Gradio SDK

Browse files
Files changed (2) hide show
  1. Dockerfile +0 -19
  2. app.py +19 -41
Dockerfile DELETED
@@ -1,19 +0,0 @@
1
- FROM python:3.12-slim
2
-
3
- WORKDIR /app
4
-
5
- COPY requirements.txt .
6
- RUN pip install --no-cache-dir -r requirements.txt
7
-
8
- COPY . .
9
-
10
- # Create directory for SQLite db
11
- RUN mkdir -p /data
12
-
13
- # Set logging to stdout for container logs
14
- ENV PYTHONUNBUFFERED=1
15
-
16
- # HuggingFace Spaces requires port 7860
17
- EXPOSE 7860
18
-
19
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,58 +1,42 @@
1
  """
2
  MTXtyle Bot - Hugging Face Spaces Entry Point (Gradio SDK)
3
  Runs the Telegram bot in a background thread and provides
4
- a Gradio status interface on port 7860.
5
  """
6
  import threading
7
- import subprocess
8
- import sys
9
  import os
10
  import time
11
  import gradio as gr
12
 
13
  bot_status = "⏳ Ishga tushirilmoqda..."
14
- bot_log = []
15
 
16
  def run_bot():
17
- global bot_status, bot_log
18
 
19
  token = os.getenv("BOT_TOKEN")
20
  if not token:
21
  bot_status = "❌ BOT_TOKEN topilmadi! Settings -> Secrets ga qo'shing."
22
- bot_log.append("ERROR: BOT_TOKEN environment variable is not set!")
23
  return
24
 
25
- max_retries = 5
26
- for attempt in range(1, max_retries + 1):
27
- bot_status = f"πŸ”„ Ishga tushirilmoqda... (urinish {attempt}/{max_retries})"
28
- bot_log.append(f"[Attempt {attempt}] Starting bot...")
29
-
30
  time.sleep(3)
31
 
32
- result = subprocess.run(
33
- [sys.executable, "bot.py"],
34
- capture_output=True,
35
- text=True
36
- )
37
 
38
- if result.returncode == 0:
39
- bot_status = "βœ… Bot ishlayapti!"
40
- break
41
- else:
42
- error_msg = result.stderr[-500:] if result.stderr else "Unknown error"
43
- bot_log.append(f"Attempt {attempt} failed: {error_msg}")
44
- bot_status = f"⚠️ Xato (urinish {attempt}), qayta urinilmoqda..."
45
- time.sleep(5 * attempt)
46
- else:
47
- bot_status = "❌ Bot ishga tushmadi. Loglarni tekshiring."
48
 
49
  def get_status():
50
- log_text = "\n".join(bot_log[-20:]) if bot_log else "Loglar hali yo'q..."
51
- return bot_status, log_text
52
-
53
- def check_status(dummy=None):
54
- status, logs = get_status()
55
- return status, logs
56
 
57
  # Start bot in background
58
  bot_thread = threading.Thread(target=run_bot, daemon=True)
@@ -63,15 +47,9 @@ with gr.Blocks(title="MTXtyle Bot") as demo:
63
  gr.Markdown("# πŸ€– MTXtyle Telegram Bot")
64
  gr.Markdown("Kiyim va aksessuarlar do'koni uchun Telegram bot")
65
 
66
- with gr.Row():
67
- status_box = gr.Textbox(label="Bot Holati", value=bot_status, interactive=False)
68
-
69
- with gr.Row():
70
- log_box = gr.Textbox(label="Loglar", value="", lines=10, interactive=False)
71
-
72
  refresh_btn = gr.Button("πŸ”„ Yangilash")
73
- refresh_btn.click(fn=check_status, outputs=[status_box, log_box])
74
-
75
- demo.load(fn=check_status, outputs=[status_box, log_box])
76
 
77
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  """
2
  MTXtyle Bot - Hugging Face Spaces Entry Point (Gradio SDK)
3
  Runs the Telegram bot in a background thread and provides
4
+ a Gradio status interface.
5
  """
6
  import threading
 
 
7
  import os
8
  import time
9
  import gradio as gr
10
 
11
  bot_status = "⏳ Ishga tushirilmoqda..."
 
12
 
13
  def run_bot():
14
+ global bot_status
15
 
16
  token = os.getenv("BOT_TOKEN")
17
  if not token:
18
  bot_status = "❌ BOT_TOKEN topilmadi! Settings -> Secrets ga qo'shing."
 
19
  return
20
 
21
+ # Import and run bot directly in thread
22
+ try:
23
+ bot_status = "πŸ”„ Bot yuklanmoqda..."
 
 
24
  time.sleep(3)
25
 
26
+ # Import bot module
27
+ import asyncio
28
+ from bot import main as bot_main
29
+
30
+ bot_status = "βœ… Bot ishlayapti!"
31
 
32
+ # Run the bot
33
+ bot_main()
34
+
35
+ except Exception as e:
36
+ bot_status = f"❌ Xato: {str(e)[:200]}"
 
 
 
 
 
37
 
38
  def get_status():
39
+ return bot_status
 
 
 
 
 
40
 
41
  # Start bot in background
42
  bot_thread = threading.Thread(target=run_bot, daemon=True)
 
47
  gr.Markdown("# πŸ€– MTXtyle Telegram Bot")
48
  gr.Markdown("Kiyim va aksessuarlar do'koni uchun Telegram bot")
49
 
50
+ status_box = gr.Textbox(label="Bot Holati", value=bot_status, interactive=False)
 
 
 
 
 
51
  refresh_btn = gr.Button("πŸ”„ Yangilash")
52
+ refresh_btn.click(fn=get_status, outputs=[status_box])
53
+ demo.load(fn=get_status, outputs=[status_box])
 
54
 
55
  demo.launch(server_name="0.0.0.0", server_port=7860)