Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,58 @@
|
|
|
|
1 |
import logging
|
2 |
-
import threading
|
3 |
-
import google.generativeai as genai
|
4 |
-
from telegram import Update
|
5 |
-
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
6 |
from flask import Flask
|
|
|
|
|
|
|
7 |
|
8 |
# ==== CONFIG ====
|
9 |
TELEGRAM_TOKEN = "7745816717:AAGKTpRtuPknjRAIct_2kdoANpJx3ZFztrg"
|
10 |
-
GEMINI_API_KEY = "AIzaSyCq23lcvpPfig6ifq1rmt-z11vKpMvDD4I"
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
"gemini-1.5-flash",
|
16 |
-
system_instruction=(
|
17 |
-
"You are Sumit, a friendly and helpful buddy inside a Telegram chat. "
|
18 |
-
"Never mention that you are an AI or Gemini. "
|
19 |
-
"Talk in a casual, human-like, friendly Hinglish style. "
|
20 |
-
"Keep answers clear, supportive, and fun."
|
21 |
-
)
|
22 |
)
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
|
|
26 |
|
27 |
-
# ====
|
28 |
-
|
29 |
-
await update.message.reply_text(
|
30 |
-
"Hey 👋, main Sumit hoon! Mujhse kuch bhi pucho – life, gyaan, ya mazedar baatein. 😊"
|
31 |
-
)
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
|
|
36 |
try:
|
37 |
-
response = model.generate_content(
|
38 |
-
reply = response.text if response.text else "
|
39 |
except Exception as e:
|
40 |
-
|
41 |
-
|
42 |
await update.message.reply_text(reply)
|
43 |
|
44 |
-
# ====
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
@app.route("/healthz")
|
52 |
-
def health():
|
53 |
-
return {"status": "ok", "bot": "Sumit is running"}
|
54 |
-
|
55 |
-
def run_flask():
|
56 |
-
# Bind to 0.0.0.0 so HF Spaces can access it
|
57 |
-
app.run(host="0.0.0.0", port=7860)
|
58 |
|
59 |
# ==== MAIN ====
|
60 |
-
def main():
|
61 |
-
# Run Flask in a separate thread
|
62 |
-
threading.Thread(target=run_flask, daemon=True).start()
|
63 |
-
|
64 |
-
# Start Telegram bot
|
65 |
-
tg_app = Application.builder().token(TELEGRAM_TOKEN).build()
|
66 |
-
tg_app.add_handler(CommandHandler("start", start))
|
67 |
-
tg_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
68 |
-
|
69 |
-
print("✅ Sumit bot is running...")
|
70 |
-
tg_app.run_polling()
|
71 |
-
|
72 |
if __name__ == "__main__":
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import logging
|
|
|
|
|
|
|
|
|
3 |
from flask import Flask
|
4 |
+
from telegram import Update
|
5 |
+
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters
|
6 |
+
import google.generativeai as genai
|
7 |
|
8 |
# ==== CONFIG ====
|
9 |
TELEGRAM_TOKEN = "7745816717:AAGKTpRtuPknjRAIct_2kdoANpJx3ZFztrg"
|
10 |
+
GEMINI_API_KEY = "AIzaSyCq23lcvpPfig6ifq1rmt-z11vKpMvDD4I"
|
11 |
|
12 |
+
# ==== LOGGING ====
|
13 |
+
logging.basicConfig(
|
14 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
)
|
16 |
+
logger = logging.getLogger(__name__)
|
17 |
|
18 |
+
# ==== GEMINI AI SETUP ====
|
19 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
+
model = genai.GenerativeModel("gemini-pro")
|
21 |
|
22 |
+
# ==== FLASK APP ====
|
23 |
+
flask_app = Flask(__name__)
|
|
|
|
|
|
|
24 |
|
25 |
+
@flask_app.route("/")
|
26 |
+
def home():
|
27 |
+
return "✅ Telegram AI Chatbot is running on port 7860!"
|
28 |
+
|
29 |
+
# ==== TELEGRAM BOT HANDLERS ====
|
30 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
31 |
+
await update.message.reply_text("👋 Hi! I am your AI friend. How can I help you today?")
|
32 |
|
33 |
+
async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
34 |
+
user_message = update.message.text
|
35 |
try:
|
36 |
+
response = model.generate_content(user_message)
|
37 |
+
reply = response.text if response.text else "⚠️ I couldn’t generate a reply."
|
38 |
except Exception as e:
|
39 |
+
logger.error(e)
|
40 |
+
reply = "❌ Something went wrong while generating response."
|
41 |
await update.message.reply_text(reply)
|
42 |
|
43 |
+
# ==== TELEGRAM BOT RUNNER ====
|
44 |
+
def run_telegram_bot():
|
45 |
+
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
|
46 |
+
app.add_handler(CommandHandler("start", start))
|
47 |
+
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, chat))
|
48 |
+
app.run_polling()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# ==== MAIN ====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
if __name__ == "__main__":
|
52 |
+
import threading
|
53 |
+
|
54 |
+
# Run Telegram bot in a separate thread
|
55 |
+
threading.Thread(target=run_telegram_bot, daemon=True).start()
|
56 |
+
|
57 |
+
# Run Flask server on 0.0.0.0:7860
|
58 |
+
flask_app.run(host="0.0.0.0", port=7860)
|