import gradio as gr import os import json import sqlite3 import hashlib import datetime from pathlib import Path from huggingface_hub import InferenceClient # Initialize HuggingFace Inference Client for real AI responses HF_TOKEN = os.getenv( "HF_TOKEN", "" ) # Set in HuggingFace Space Settings -> Repository Secrets inference_client = InferenceClient(token=HF_TOKEN if HF_TOKEN else None) # Cloudflare configuration - credentials from wrangler.toml and CLI CLOUDFLARE_CONFIG = { "api_token": os.getenv("CLOUDFLARE_API_TOKEN", ""), "account_id": os.getenv( "CLOUDFLARE_ACCOUNT_ID", "62af59a7ac82b29543577ee6800735ee" ), "d1_database_id": os.getenv( "CLOUDFLARE_D1_DATABASE_ID", "6d887f74-98ac-4db7-bfed-8061903d1f6c" ), "r2_bucket_name": os.getenv("CLOUDFLARE_R2_BUCKET_NAME", "openmanus-storage"), "kv_namespace_id": os.getenv( "CLOUDFLARE_KV_NAMESPACE_ID", "87f4aa01410d4fb19821f61006f94441" ), "kv_namespace_cache": os.getenv( "CLOUDFLARE_KV_CACHE_ID", "7b58c88292c847d1a82c8e0dd5129f37" ), "durable_objects_sessions": "AGENT_SESSIONS", "durable_objects_chatrooms": "CHAT_ROOMS", } # AI Model Categories with 200+ models AI_MODELS = { "Text Generation": { "Qwen Models": [ "Qwen/Qwen2.5-72B-Instruct", "Qwen/Qwen2.5-32B-Instruct", "Qwen/Qwen2.5-14B-Instruct", "Qwen/Qwen2.5-7B-Instruct", "Qwen/Qwen2.5-3B-Instruct", "Qwen/Qwen2.5-1.5B-Instruct", "Qwen/Qwen2.5-0.5B-Instruct", "Qwen/Qwen2-72B-Instruct", "Qwen/Qwen2-57B-A14B-Instruct", "Qwen/Qwen2-7B-Instruct", "Qwen/Qwen2-1.5B-Instruct", "Qwen/Qwen2-0.5B-Instruct", "Qwen/Qwen1.5-110B-Chat", "Qwen/Qwen1.5-72B-Chat", "Qwen/Qwen1.5-32B-Chat", "Qwen/Qwen1.5-14B-Chat", "Qwen/Qwen1.5-7B-Chat", "Qwen/Qwen1.5-4B-Chat", "Qwen/Qwen1.5-1.8B-Chat", "Qwen/Qwen1.5-0.5B-Chat", "Qwen/CodeQwen1.5-7B-Chat", "Qwen/Qwen2.5-Math-72B-Instruct", "Qwen/Qwen2.5-Math-7B-Instruct", "Qwen/Qwen2.5-Coder-32B-Instruct", "Qwen/Qwen2.5-Coder-14B-Instruct", "Qwen/Qwen2.5-Coder-7B-Instruct", "Qwen/Qwen2.5-Coder-3B-Instruct", "Qwen/Qwen2.5-Coder-1.5B-Instruct", "Qwen/Qwen2.5-Coder-0.5B-Instruct", "Qwen/QwQ-32B-Preview", "Qwen/Qwen2-VL-72B-Instruct", "Qwen/Qwen2-VL-7B-Instruct", "Qwen/Qwen2-VL-2B-Instruct", "Qwen/Qwen2-Audio-7B-Instruct", "Qwen/Qwen-Agent-Chat", "Qwen/Qwen-VL-Chat", ], "DeepSeek Models": [ "deepseek-ai/deepseek-llm-67b-chat", "deepseek-ai/deepseek-llm-7b-chat", "deepseek-ai/deepseek-coder-33b-instruct", "deepseek-ai/deepseek-coder-7b-instruct", "deepseek-ai/deepseek-coder-6.7b-instruct", "deepseek-ai/deepseek-coder-1.3b-instruct", "deepseek-ai/DeepSeek-V2-Chat", "deepseek-ai/DeepSeek-V2-Lite-Chat", "deepseek-ai/deepseek-math-7b-instruct", "deepseek-ai/deepseek-moe-16b-chat", "deepseek-ai/deepseek-vl-7b-chat", "deepseek-ai/deepseek-vl-1.3b-chat", "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B", "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "deepseek-ai/DeepSeek-R1-Distill-Llama-8B", "deepseek-ai/DeepSeek-Reasoner-R1", ], }, "Image Processing": { "Image Generation": [ "black-forest-labs/FLUX.1-dev", "black-forest-labs/FLUX.1-schnell", "black-forest-labs/FLUX.1-pro", "runwayml/stable-diffusion-v1-5", "stabilityai/stable-diffusion-xl-base-1.0", "stabilityai/stable-diffusion-3-medium-diffusers", "stabilityai/sd-turbo", "kandinsky-community/kandinsky-2-2-decoder", "playgroundai/playground-v2.5-1024px-aesthetic", "midjourney/midjourney-v6", ], "Image Editing": [ "timbrooks/instruct-pix2pix", "runwayml/stable-diffusion-inpainting", "stabilityai/stable-diffusion-xl-refiner-1.0", "lllyasviel/control_v11p_sd15_inpaint", "SG161222/RealVisXL_V4.0", "ByteDance/SDXL-Lightning", "segmind/SSD-1B", "segmind/Segmind-Vega", "playgroundai/playground-v2-1024px-aesthetic", "stabilityai/stable-cascade", "lllyasviel/ControlNet-v1-1", "lllyasviel/sd-controlnet-canny", "Monster-Labs/control_v1p_sd15_qrcode_monster", "TencentARC/PhotoMaker", "instantX/InstantID", ], "Face Processing": [ "InsightFace/inswapper_128.onnx", "deepinsight/insightface", "TencentARC/GFPGAN", "sczhou/CodeFormer", "xinntao/Real-ESRGAN", "ESRGAN/ESRGAN", ], }, "Video Generation": { "Text-to-Video": [ "ali-vilab/text-to-video-ms-1.7b", "damo-vilab/text-to-video-ms-1.7b", "modelscope/text-to-video-synthesis", "camenduru/potat1", "stabilityai/stable-video-diffusion-img2vid", "stabilityai/stable-video-diffusion-img2vid-xt", "ByteDance/AnimateDiff", "guoyww/animatediff", ], "Image-to-Video": [ "stabilityai/stable-video-diffusion-img2vid", "stabilityai/stable-video-diffusion-img2vid-xt-1-1", "TencentARC/MotionCtrl", "ali-vilab/i2vgen-xl", "Doubiiu/ToonCrafter", ], "Video Editing": [ "MCG-NJU/VideoMAE", "showlab/Tune-A-Video", "Picsart-AI-Research/Text2Video-Zero", "damo-vilab/MS-Vid2Vid-XL", "kabachuha/sd-webui-deforum", ], }, "AI Teacher & Education": { "Math & Science": [ "Qwen/Qwen2.5-Math-72B-Instruct", "Qwen/Qwen2.5-Math-7B-Instruct", "deepseek-ai/deepseek-math-7b-instruct", "mistralai/Mistral-Math-7B-v0.1", "WizardLM/WizardMath-70B-V1.0", "MathGPT/MathGPT-32B", ], "Coding Tutor": [ "Qwen/Qwen2.5-Coder-32B-Instruct", "deepseek-ai/deepseek-coder-33b-instruct", "WizardLM/WizardCoder-Python-34B-V1.0", "bigcode/starcoder2-15b-instruct-v0.1", "meta-llama/CodeLlama-34b-Instruct-hf", ], "Language Learning": [ "facebook/nllb-200-3.3B", "facebook/seamless-m4t-v2-large", "Helsinki-NLP/opus-mt-multilingual", "google/madlad400-10b-mt", "Unbabel/TowerInstruct-7B-v0.1", ], "General Education": [ "Qwen/Qwen2.5-72B-Instruct", "microsoft/Phi-3-medium-128k-instruct", "mistralai/Mistral-7B-Instruct-v0.3", "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", "openchat/openchat-3.5-1210", ], }, "Software Engineer Agent": { "Code Generation": [ "Qwen/Qwen2.5-Coder-32B-Instruct", "Qwen/Qwen2.5-Coder-14B-Instruct", "Qwen/Qwen2.5-Coder-7B-Instruct", "deepseek-ai/deepseek-coder-33b-instruct", "deepseek-ai/deepseek-coder-7b-instruct", "deepseek-ai/deepseek-coder-6.7b-instruct", "meta-llama/CodeLlama-70b-Instruct-hf", "meta-llama/CodeLlama-34b-Instruct-hf", "meta-llama/CodeLlama-13b-Instruct-hf", "meta-llama/CodeLlama-7b-Instruct-hf", ], "Code Analysis & Review": [ "bigcode/starcoder2-15b-instruct-v0.1", "bigcode/starcoder2-7b", "bigcode/starcoderbase-7b", "WizardLM/WizardCoder-Python-34B-V1.0", "WizardLM/WizardCoder-15B-V1.0", "Phind/Phind-CodeLlama-34B-v2", "codellama/CodeLlama-70b-Python-hf", ], "Specialized Coding": [ "Salesforce/codegen25-7b-multi", "Salesforce/codegen-16B-multi", "replit/replit-code-v1-3b", "NinedayWang/PolyCoder-2.7B", "stabilityai/stablelm-base-alpha-7b-v2", "teknium/OpenHermes-2.5-Mistral-7B", ], "DevOps & Infrastructure": [ "deepseek-ai/deepseek-coder-33b-instruct", "Qwen/Qwen2.5-Coder-32B-Instruct", "mistralai/Mistral-7B-Instruct-v0.3", "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO", ], }, "Audio Processing": { "Text-to-Speech": [ "microsoft/speecht5_tts", "facebook/mms-tts-eng", "facebook/mms-tts-ara", "coqui/XTTS-v2", "suno/bark", "parler-tts/parler-tts-large-v1", "microsoft/DisTTS", "facebook/fastspeech2-en-ljspeech", "espnet/kan-bayashi_ljspeech_vits", "facebook/tts_transformer-en-ljspeech", "microsoft/SpeechT5", "Voicemod/fastspeech2-en-male1", "facebook/mms-tts-spa", "facebook/mms-tts-fra", "facebook/mms-tts-deu", ], "Speech-to-Text": [ "openai/whisper-large-v3", "openai/whisper-large-v2", "openai/whisper-medium", "openai/whisper-small", "openai/whisper-base", "openai/whisper-tiny", "facebook/wav2vec2-large-960h", "facebook/wav2vec2-base-960h", "microsoft/unispeech-sat-large", "nvidia/stt_en_conformer_ctc_large", "speechbrain/asr-wav2vec2-commonvoice-en", "facebook/mms-1b-all", "facebook/seamless-m4t-v2-large", "distil-whisper/distil-large-v3", "distil-whisper/distil-medium.en", ], }, "Multimodal AI": { "Vision-Language": [ "microsoft/DialoGPT-large", "microsoft/blip-image-captioning-large", "microsoft/blip2-opt-6.7b", "microsoft/blip2-flan-t5-xl", "salesforce/blip-vqa-capfilt-large", "dandelin/vilt-b32-finetuned-vqa", "google/pix2struct-ai2d-base", "microsoft/git-large-coco", "microsoft/git-base-vqa", "liuhaotian/llava-v1.6-34b", "liuhaotian/llava-v1.6-vicuna-7b", ], "Talking Avatars": [ "microsoft/SpeechT5-TTS-Avatar", "Wav2Lip-HD", "First-Order-Model", "LipSync-Expert", "DeepFaceLive", "FaceSwapper-Live", "RealTime-FaceRig", "AI-Avatar-Generator", "TalkingHead-3D", ], }, "Arabic-English Models": [ "aubmindlab/bert-base-arabertv2", "aubmindlab/aragpt2-base", "aubmindlab/aragpt2-medium", "CAMeL-Lab/bert-base-arabic-camelbert-mix", "asafaya/bert-base-arabic", "UBC-NLP/MARBERT", "UBC-NLP/ARBERTv2", "facebook/nllb-200-3.3B", "facebook/m2m100_1.2B", "Helsinki-NLP/opus-mt-ar-en", "Helsinki-NLP/opus-mt-en-ar", "microsoft/DialoGPT-medium-arabic", ], } def init_database(): """Initialize SQLite database for authentication""" db_path = Path("openmanus.db") conn = sqlite3.connect(db_path) cursor = conn.cursor() # Create users table cursor.execute( """ CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_number TEXT UNIQUE NOT NULL, full_name TEXT NOT NULL, password_hash TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_login TIMESTAMP, is_active BOOLEAN DEFAULT 1 ) """ ) # Create sessions table cursor.execute( """ CREATE TABLE IF NOT EXISTS sessions ( id TEXT PRIMARY KEY, user_id INTEGER NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMP NOT NULL, ip_address TEXT, user_agent TEXT, FOREIGN KEY (user_id) REFERENCES users (id) ) """ ) # Create model usage table cursor.execute( """ CREATE TABLE IF NOT EXISTS model_usage ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, model_name TEXT NOT NULL, category TEXT NOT NULL, input_text TEXT, output_text TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, processing_time REAL, FOREIGN KEY (user_id) REFERENCES users (id) ) """ ) conn.commit() conn.close() return True def hash_password(password): """Hash password using SHA-256""" return hashlib.sha256(password.encode()).hexdigest() def signup_user(mobile, name, password, confirm_password): """User registration with mobile number""" if not all([mobile, name, password, confirm_password]): return "❌ Please fill in all fields" if password != confirm_password: return "❌ Passwords do not match" if len(password) < 6: return "❌ Password must be at least 6 characters" # Validate mobile number if not mobile.replace("+", "").replace("-", "").replace(" ", "").isdigit(): return "❌ Please enter a valid mobile number" try: conn = sqlite3.connect("openmanus.db") cursor = conn.cursor() # Check if mobile number already exists cursor.execute("SELECT id FROM users WHERE mobile_number = ?", (mobile,)) if cursor.fetchone(): conn.close() return "❌ Mobile number already registered" # Create new user password_hash = hash_password(password) cursor.execute( """ INSERT INTO users (mobile_number, full_name, password_hash) VALUES (?, ?, ?) """, (mobile, name, password_hash), ) conn.commit() conn.close() return f"✅ Account created successfully for {name}! Welcome to OpenManus Platform." except Exception as e: return f"❌ Registration failed: {str(e)}" def login_user(mobile, password): """User authentication""" if not mobile or not password: return "❌ Please provide mobile number and password" try: conn = sqlite3.connect("openmanus.db") cursor = conn.cursor() # Verify credentials password_hash = hash_password(password) cursor.execute( """ SELECT id, full_name FROM users WHERE mobile_number = ? AND password_hash = ? AND is_active = 1 """, (mobile, password_hash), ) user = cursor.fetchone() if user: # Update last login cursor.execute( """ UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ? """, (user[0],), ) conn.commit() conn.close() return f"✅ Welcome back, {user[1]}! Login successful." else: conn.close() return "❌ Invalid mobile number or password" except Exception as e: return f"❌ Login failed: {str(e)}" def use_ai_model(model_name, input_text, user_session="guest"): """Use real HuggingFace Inference API to process prompts with AI models""" if not input_text.strip(): return "Please enter some text for the AI model to process." model_lower = model_name.lower() # Determine model category for specialized handling category = "text" if any( x in model_lower for x in ["codellama", "starcoder", "codegen", "replit", "polycoder", "coder"] ): category = "software_engineer" elif any( x in model_lower for x in ["flux", "diffusion", "stable-diffusion", "sdxl", "kandinsky"] ): category = "image_gen" elif any( x in model_lower for x in ["pix2pix", "inpaint", "controlnet", "photomaker", "instantid"] ): category = "image_edit" elif ( any( x in model_lower for x in ["math", "teacher", "education", "translate", "wizard"] ) and "coder" not in model_lower ): category = "education" elif any( x in model_lower for x in ["tts", "speech", "audio", "whisper", "wav2vec", "bark"] ): category = "audio" elif any( x in model_lower for x in [ "face", "avatar", "talking", "wav2lip", "vl", "blip", "vision", "llava", ] ): category = "multimodal" try: # Use HuggingFace Inference API for REAL AI responses if category in ["image_gen", "image_edit"]: response = f"🎨 {model_name} is generating your image...\n\n" response += f"📸 Prompt: '{input_text}'\n\n" response += f"ℹ️ Image generation models require special handling. " response += f"The model '{model_name}' will create an image based on your prompt.\n\n" response += ( f"💡 To view the generated image, use the Image Generation interface." ) return response elif category == "audio": response = f"🎵 {model_name} audio processing...\n\n" response += f"Input: '{input_text}'\n\n" response += ( f"ℹ️ Audio models require audio file input or special parameters. " ) response += ( f"Please use the Audio Processing interface for full functionality." ) return response else: # Text-based models messages = [] if category == "software_engineer": messages.append( { "role": "system", "content": "You are an expert software engineer. Provide production-ready code with best practices, error handling, and clear documentation.", } ) elif category == "education": messages.append( { "role": "system", "content": "You are an expert AI teacher. Provide clear, step-by-step explanations with examples to help students understand.", } ) elif category == "multimodal": messages.append( { "role": "system", "content": "You are a multimodal AI assistant capable of understanding and describing visual content and complex queries.", } ) messages.append({"role": "user", "content": input_text}) # Call HuggingFace Inference API full_response = "" try: for message in inference_client.chat_completion( model=model_name, messages=messages, max_tokens=2000, temperature=0.7, stream=True, ): if message.choices and message.choices[0].delta.content: full_response += message.choices[0].delta.content if not full_response: full_response = ( "Model response was empty. Try rephrasing your prompt." ) icons = { "software_engineer": "💻", "education": "🎓", "multimodal": "🤖", "text": "🧠", } icon = icons.get(category, "✨") return f"{icon} **{model_name}**\n\n{full_response}" except Exception as e: error_msg = str(e) if "404" in error_msg or "not found" in error_msg.lower(): return f"⚠️ Model '{model_name}' is not available via Inference API.\n\nTry using a popular model like:\n- Qwen/Qwen2.5-72B-Instruct\n- meta-llama/Llama-3.3-70B-Instruct\n- mistralai/Mistral-7B-Instruct-v0.3" elif "rate limit" in error_msg.lower(): return f"⏱️ Rate limit reached. Please:\n1. Wait a moment and try again\n2. Add your HF_TOKEN in Space settings for higher limits\n3. Use a different model\n\nError: {error_msg}" else: return f"❌ Error calling {model_name}:\n{error_msg}\n\nTry:\n1. Check if model name is correct\n2. Try a different model\n3. Add HF_TOKEN for authentication" except Exception as e: return f"❌ Unexpected error: {str(e)}\n\nPlease try again or use a different model." def get_cloudflare_status(): """Get Cloudflare services status""" services = [] if CLOUDFLARE_CONFIG["d1_database_id"]: services.append("✅ D1 Database Connected") else: services.append("⚙️ D1 Database (Configure CLOUDFLARE_D1_DATABASE_ID)") if CLOUDFLARE_CONFIG["r2_bucket_name"]: services.append("✅ R2 Storage Connected") else: services.append("⚙️ R2 Storage (Configure CLOUDFLARE_R2_BUCKET_NAME)") if CLOUDFLARE_CONFIG["kv_namespace_id"]: services.append("✅ KV Sessions Connected") else: services.append("⚙️ KV Sessions (Configure CLOUDFLARE_KV_NAMESPACE_ID)") if CLOUDFLARE_CONFIG["kv_namespace_cache"]: services.append("✅ KV Cache Connected") else: services.append("⚙️ KV Cache (Configure CLOUDFLARE_KV_CACHE_ID)") if CLOUDFLARE_CONFIG["durable_objects_sessions"]: services.append("✅ Durable Objects (Agent Sessions)") if CLOUDFLARE_CONFIG["durable_objects_chatrooms"]: services.append("✅ Durable Objects (Chat Rooms)") return "\n".join(services) # Initialize database init_database() # Create Gradio interface with gr.Blocks( title="OpenManus - Complete AI Platform", theme=gr.themes.Soft(), css=""" .container { max-width: 1400px; margin: 0 auto; } .header { text-align: center; padding: 25px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 15px; margin-bottom: 25px; } .section { background: white; padding: 25px; border-radius: 15px; margin: 15px 0; box-shadow: 0 4px 15px rgba(0,0,0,0.1); } """, ) as app: # Header gr.HTML( """
Mobile Authentication + 200+ AI Models + Cloudflare Services
🧠 Qwen & DeepSeek | 🖼️ Image Processing | 🎵 TTS/STT | 👤 Face Swap | 🌍 Arabic-English | ☁️ Cloud Integration
CLOUDFLARE_API_TOKEN - API authenticationCLOUDFLARE_ACCOUNT_ID - Account identifierCLOUDFLARE_D1_DATABASE_ID - D1 databaseCLOUDFLARE_R2_BUCKET_NAME - R2 storageCLOUDFLARE_KV_NAMESPACE_ID - KV cacheCLOUDFLARE_DURABLE_OBJECTS_ID - Durable objectsComplete AI Platform successfully deployed on HuggingFace Spaces!