o7c1 commited on
Commit
feece21
·
verified ·
1 Parent(s): a198e7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -1,25 +1,24 @@
1
  import os
 
 
2
  import google.generativeai as genai
3
  from balethon import Client
4
- from flask import Flask, request
5
 
6
- # --- Configuration ---
7
- # IMPORTANT: Set these as "Secrets" in your Hugging Face Space settings.
8
- BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
9
- GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
10
 
11
  # --- Initialize AI Model ---
12
  genai.configure(api_key=GOOGLE_API_KEY)
13
  model = genai.GenerativeModel('gemma-4-31b-it')
14
 
15
- # --- Initialize Bale Bot ---
16
- # The Balethon library works by polling for updates.
17
  bot = Client(BALE_BOT_TOKEN)
18
 
19
- # --- Bot Logic ---
20
  @bot.on_message()
21
  async def handle_message(message):
22
- """Receives a user message, gets a response from Gemma, and replies."""
23
  try:
24
  user_text = message.text
25
  if not user_text:
@@ -29,14 +28,29 @@ async def handle_message(message):
29
  response = model.generate_content(user_text)
30
  ai_response = response.text
31
 
32
- # Send the response back to the user on Bale
33
  await message.reply(ai_response)
34
-
35
  except Exception as e:
36
- print(f"An error occurred: {e}")
37
- await message.reply("Sorry, I ran into an error. Please try again.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # --- Run the Bot ---
40
  if __name__ == "__main__":
41
- print("Bale bot is starting...")
42
- bot.run()
 
 
 
 
1
  import os
2
+ import asyncio
3
+ import threading
4
  import google.generativeai as genai
5
  from balethon import Client
6
+ from flask import Flask
7
 
8
+ # --- Configuration (set as Secrets in your Space) ---
9
+ BALE_BOT_TOKEN = os.environ["BALE_BOT_TOKEN"]
10
+ GOOGLE_API_KEY = os.environ["GOOGLE_API_KEY"]
 
11
 
12
  # --- Initialize AI Model ---
13
  genai.configure(api_key=GOOGLE_API_KEY)
14
  model = genai.GenerativeModel('gemma-4-31b-it')
15
 
16
+ # --- Initialize Bale Bot (but don't start it yet) ---
 
17
  bot = Client(BALE_BOT_TOKEN)
18
 
19
+ # --- Bot Message Handler ---
20
  @bot.on_message()
21
  async def handle_message(message):
 
22
  try:
23
  user_text = message.text
24
  if not user_text:
 
28
  response = model.generate_content(user_text)
29
  ai_response = response.text
30
 
31
+ # Reply on Bale
32
  await message.reply(ai_response)
 
33
  except Exception as e:
34
+ print(f"Error: {e}")
35
+ await message.reply("Sorry, I ran into an error.")
36
+
37
+ # --- Background Thread for Polling ---
38
+ def start_polling():
39
+ """Run the bot's event loop in a separate thread."""
40
+ loop = asyncio.new_event_loop()
41
+ asyncio.set_event_loop(loop)
42
+ bot.run() # This blocks inside the thread
43
+
44
+ # --- Minimal Flask App for Health Checks ---
45
+ app = Flask(__name__)
46
+
47
+ @app.route('/')
48
+ def health():
49
+ return "Bot is alive", 200
50
 
 
51
  if __name__ == "__main__":
52
+ # Start the bot in a background thread
53
+ threading.Thread(target=start_polling, daemon=True).start()
54
+
55
+ # Start the Flask web server (this keeps the container alive)
56
+ app.run(host="0.0.0.0", port=7860)