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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
26
+ return
27
+
28
+ # Call the Google AI Studio API
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()