import os import gradio as gr from openai import OpenAI import requests import random # ============================================ # NEXUS AI - GRADIO 6 COMPATIBLE # ============================================ api_key = os.getenv("OPENROUTER_API_KEY") client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=api_key ) SYSTEM_CHAT = """You are Nexus AI, a brilliant friendly personal AI assistant. Help with anything — questions, planning, analysis, writing, advice. Be helpful, concise and smart.""" SYSTEM_CODE = """You are Nexus AI Code — an elite software engineer. Write complete, production-ready, well-commented code. Always use markdown code blocks with language specified. Support all languages and frameworks.""" # ============================================ # FUNCTIONS # ============================================ def generate_response(message, history, system_prompt): try: if not api_key: return "❌ OPENROUTER_API_KEY not found in Hugging Face Secrets." history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": response}) for item in history: messages.append({"role": "user", "content": item[0]}) messages.append({"role": "assistant", "content": item[1]}) messages.append({"role": "user", "content": message}) completion = client.chat.completions.create( model="deepseek/deepseek-chat-v3-0324:free", messages=messages, temperature=0.7, max_tokens=2000 ) return completion.choices[0].message.content except Exception as e: return f"❌ Error: {str(e)}" def chat(message, history): if history is None: history = [] if not message.strip(): return "", history response = generate_response(message, history, SYSTEM_CHAT) history.append((message, response)) return "", history def code_chat(message, history): if history is None: history = [] if not message.strip(): return "", history response = generate_response(message, history, SYSTEM_CODE) history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": response}) return "", history def generate_image(prompt, style): if not prompt.strip(): return None, "❌ Please describe your image first!" style_map = { "Default": "", "Photo": "photorealistic, DSLR, 8k", "Anime": "anime style, studio ghibli", "Painting": "oil painting, impressionist", "Digital Art": "cinematic digital art, concept art", "3D Render": "3d render, octane render, unreal engine", "Pixel Art": "pixel art, 16-bit", "Cyberpunk": "neon glow, cyberpunk, futuristic" } style_text = style_map.get(style, "") full_prompt = f"{prompt}, {style_text}, high quality, ultra detailed" if style_text else f"{prompt}, high quality, ultra detailed" seed = random.randint(0, 9999999) url = f"https://image.pollinations.ai/prompt/{requests.utils.quote(full_prompt)}?width=512&height=512&seed={seed}&nologo=true&enhance=true" try: response = requests.get(url, timeout=30) if response.status_code == 200: from PIL import Image from io import BytesIO img = Image.open(BytesIO(response.content)) return img, "✅ Image generated!" else: return None, "❌ Failed. Try again!" except Exception as e: return None, f"❌ Error: {str(e)}" # ============================================ # CSS # ============================================ css = """ .gradio-container { max-width: 900px !important; margin: auto !important; } #title { text-align: center; font-size: 36px; font-weight: 800; color: #7c3aed; margin-bottom: 4px; } #subtitle { text-align: center; color: #94a3b8; font-size: 14px; margin-bottom: 16px; } footer { display: none !important; } """ # ============================================ # UI - GRADIO 6 COMPATIBLE # ============================================ with gr.Blocks() as demo: gr.HTML("""