Spaces:
Sleeping

File size: 1,750 Bytes
7ef3b3d
 
 
 
38a37e2
7ef3b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98e8a6f
 
38a37e2
 
 
 
7ef3b3d
 
 
38a37e2
7ef3b3d
 
38a37e2
7ef3b3d
 
bb7a052
38a37e2
7ef3b3d
 
38a37e2
7ef3b3d
38a37e2
7ef3b3d
38a37e2
7ef3b3d
 
 
 
38a37e2
 
7ef3b3d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import asyncio
from threading import Thread
from flask import Flask
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
from groq import Groq

# 1. Flask App for Hugging Face Health Check
webapp = Flask(__name__)

@webapp.route('/')
def health():
    return "Maya is alive and running!"

def run_web():
    # Hugging Face uses port 7860 by default
    webapp.run(host="0.0.0.0", port=7860)

# 2. Get API Keys from Secrets
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")


client = Groq(api_key=GROQ_API_KEY)

# 3. Bot Functions
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Hi! I am Maya. How can I help you today?")

async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_message = update.message.text
    try:
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": user_message}],
            model="llama-3.3-70b-versatile"
        )
        bot_reply = response.choices[0].message.content
        await update.message.reply_text(bot_reply)
    except Exception as e:
        await update.message.reply_text("Sorry, I'm having a bit of a headache. Try again later!")

# 4. Main Entry Point
if __name__ == '__main__':
    # Background thread mein web server start karein
    Thread(target=run_web).start()
    
    # Telegram Bot start karein
    app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
    
    app.add_handler(CommandHandler('start', start))
    app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), chat))
    
    print("Maya is starting...")
    app.run_polling()