Spaces:
Runtime error
Runtime error
Download app.py from behaviour01/deep_learning: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/behaviour01/deep_learning/resolve/main/app.py
- Command line
-
hf download hf://spaces/behaviour01/deep_learning/app.py
-
curl -L -o app.py https://huggingface.co/spaces/behaviour01/deep_learning/resolve/main/app.py
1.33 kB
| # app.py | |
| import os | |
| import asyncio | |
| import requests | |
| from fastapi import FastAPI | |
| from telegram.ext import Application, CommandHandler | |
| TELEGRAM_BOT_TOKEN = "7799719627:AAFz7gF74OeajbUqqb4ZJcsJPzal8R8NN3o" | |
| # === Region Detection === | |
| async def get_real_region(): | |
| try: | |
| res = requests.get("https://ipapi.co/json/ ", timeout=5) | |
| data = res.json() | |
| return f"{data.get('country_name', '')}, {data.get('region', '')}" | |
| except Exception: | |
| return "Unknown location" | |
| async def region_command(update, context): | |
| await update.message.reply_text(f"📍 This bot is running in: {await get_real_region()}") | |
| # === FastAPI App === | |
| app = FastAPI() | |
| async def home(): | |
| return {"message": "Bot is running. Send /region to Telegram bot."} | |
| # === Start Bot and Web Server Together === | |
| async def start_bot_and_server(): | |
| # Setup Telegram bot | |
| application = Application.builder().token(TELEGRAM_BOT_TOKEN).build() | |
| application.add_handler(CommandHandler("region", region_command)) | |
| await application.initialize() | |
| await application.start() | |
| # Setup FastAPI server | |
| import uvicorn | |
| config = uvicorn.Config(app, host="0.0.0.0", port=7860) | |
| server = uvicorn.Server(config) | |
| await server.serve() | |
| if __name__ == "__main__": | |
| asyncio.run(start_bot_and_server()) |