Deploy Bot commited on
Commit
c4cbaf0
Β·
1 Parent(s): 2af8f98

Switch from Docker to Gradio SDK for network access

Browse files
Files changed (3) hide show
  1. README.md +3 -8
  2. app.py +53 -47
  3. requirements.txt +0 -0
README.md CHANGED
@@ -3,17 +3,12 @@ title: Mtextile Bot
3
  emoji: πŸ›
4
  colorFrom: blue
5
  colorTo: purple
6
- sdk: docker
 
 
7
  pinned: false
8
  ---
9
 
10
  # MTXtyle - Telegram Bot
11
 
12
  Kiyim va aksessuarlar do'koni uchun Telegram bot.
13
-
14
- ## Features
15
- - Katalog va mahsulotlar boshqaruvi
16
- - Savat va buyurtma berish tizimi
17
- - Admin panel
18
- - Broadcast va Excel export
19
- - Rate limiting va xavfsizlik
 
3
  emoji: πŸ›
4
  colorFrom: blue
5
  colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.44.0
8
+ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
  # MTXtyle - Telegram Bot
13
 
14
  Kiyim va aksessuarlar do'koni uchun Telegram bot.
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,71 +1,77 @@
1
  """
2
- MTXtyle Bot - Hugging Face Spaces Entry Point
3
- Runs a simple HTTP health server on port 7860 (required by HF)
4
- and starts the Telegram bot in a background thread with retry logic.
5
  """
6
  import threading
7
- import time
8
  import subprocess
9
  import sys
10
  import os
11
- from http.server import HTTPServer, BaseHTTPRequestHandler
12
-
13
- bot_status = "starting"
14
 
15
- class HealthHandler(BaseHTTPRequestHandler):
16
- def do_GET(self):
17
- self.send_response(200)
18
- self.send_header("Content-type", "text/html; charset=utf-8")
19
- self.end_headers()
20
- html = f"""
21
- <html><body style="font-family:sans-serif;text-align:center;padding:50px;">
22
- <h1>πŸ€– MTXtyle Bot</h1>
23
- <p>Status: {bot_status}</p>
24
- </body></html>
25
- """
26
- self.wfile.write(html.encode("utf-8"))
27
-
28
- def log_message(self, format, *args):
29
- pass
30
 
31
  def run_bot():
32
- global bot_status
33
 
34
- # Check BOT_TOKEN
35
  token = os.getenv("BOT_TOKEN")
36
  if not token:
37
- bot_status = "❌ BOT_TOKEN not set! Add it in Space Settings -> Secrets"
38
- print("ERROR: BOT_TOKEN environment variable is not set!")
39
- print("Please add BOT_TOKEN in your HuggingFace Space Settings -> Secrets")
40
  return
41
 
42
- # Retry loop with delay
43
- max_retries = 10
44
  for attempt in range(1, max_retries + 1):
45
- bot_status = f"πŸ”„ Starting (attempt {attempt}/{max_retries})..."
46
- print(f"[Attempt {attempt}/{max_retries}] Starting bot...")
47
 
48
- # Wait for network to be ready
49
- time.sleep(5 * attempt) # Increasing delay: 5s, 10s, 15s...
50
 
51
- result = subprocess.run([sys.executable, "bot.py"])
 
 
 
 
52
 
53
  if result.returncode == 0:
54
- bot_status = "βœ… Running"
55
  break
56
  else:
57
- bot_status = f"⚠️ Crashed (attempt {attempt}), retrying..."
58
- print(f"Bot crashed with code {result.returncode}, retrying in {5 * (attempt + 1)}s...")
 
 
59
  else:
60
- bot_status = "❌ Failed after all retries"
61
- print("Bot failed to start after all retries.")
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- # Start bot in background thread with delay
65
- bot_thread = threading.Thread(target=run_bot, daemon=True)
66
- bot_thread.start()
 
 
 
 
 
 
 
 
 
 
67
 
68
- # Start health server on port 7860 (required by HuggingFace)
69
- server = HTTPServer(("0.0.0.0", 7860), HealthHandler)
70
- print("🌐 Health server running on port 7860")
71
- server.serve_forever()
 
 
 
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)
59
+ bot_thread.start()
60
+
61
+ # Gradio Interface
62
+ 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)
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ