Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
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
|
@@ -17,7 +17,8 @@ logger = logging.getLogger(__name__)
|
|
17 |
|
18 |
# ==== GEMINI AI SETUP ====
|
19 |
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
-
model
|
|
|
21 |
|
22 |
# ==== FLASK APP ====
|
23 |
flask_app = Flask(__name__)
|
@@ -28,31 +29,26 @@ def home():
|
|
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
|
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
|
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 |
-
# ====
|
44 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
58 |
-
flask_app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
1 |
import logging
|
2 |
from flask import Flask
|
3 |
+
from threading import Thread
|
4 |
from telegram import Update
|
5 |
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters
|
6 |
import google.generativeai as genai
|
|
|
17 |
|
18 |
# ==== GEMINI AI SETUP ====
|
19 |
genai.configure(api_key=GEMINI_API_KEY)
|
20 |
+
# Updated model name - gemini-pro is deprecated
|
21 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
22 |
|
23 |
# ==== FLASK APP ====
|
24 |
flask_app = Flask(__name__)
|
|
|
29 |
|
30 |
# ==== TELEGRAM BOT HANDLERS ====
|
31 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
32 |
+
await update.message.reply_text("👋 Hi! I am Sumit, your AI buddy. How can I help you today?")
|
33 |
|
34 |
async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
35 |
user_message = update.message.text
|
36 |
try:
|
37 |
response = model.generate_content(user_message)
|
38 |
+
reply = response.text if response.text else "⚠️ I couldn't generate a reply."
|
39 |
except Exception as e:
|
40 |
logger.error(e)
|
41 |
reply = "❌ Something went wrong while generating response."
|
42 |
await update.message.reply_text(reply)
|
43 |
|
44 |
+
# ==== MAIN ====
|
45 |
+
if __name__ == "__main__":
|
46 |
+
# Run Flask in a separate thread
|
47 |
+
Thread(target=lambda: flask_app.run(host="0.0.0.0", port=7860), daemon=True).start()
|
48 |
+
|
49 |
+
# Run Telegram bot (no asyncio.run!)
|
50 |
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
|
51 |
app.add_handler(CommandHandler("start", start))
|
52 |
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, chat))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
app.run_polling()
|
|