shashwatIDR commited on
Commit
d56de30
·
verified ·
1 Parent(s): c540b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
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 = genai.GenerativeModel("gemini-pro")
 
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 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 couldnt 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)
 
 
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()