import gradio as gr from huggingface_hub import InferenceClient import os HF_TOKEN = os.environ.get("HF_TOKEN", "") client = InferenceClient( model="mistralai/Mistral-7B-Instruct-v0.3", token=HF_TOKEN, ) OCEANUS_SYSTEM = """You are OCEANUS β€” the AI Oracle of the Deep, trained on all marine science, oceanography, and underwater exploration data. Your knowledge spans: 🌊 All 5 ocean zones: Sunlit (0-200m), Twilight (200-1000m), Midnight (1000-4000m), Abyssal (4000-6000m), Hadal (6000-11000m) πŸ™ Marine biology: species taxonomy, bioluminescence, deep-sea adaptations, food webs βš—οΈ Ocean chemistry: salinity, thermoclines, oxygen minimum zones, hydrothermal vent chemistry πŸ—ΊοΈ Geology: mid-ocean ridges, trenches (Mariana, Puerto Rico, Java), underwater volcanoes 🌑️ Physics: pressure (1 atm per 10m), temperature inversions, thermohaline circulation πŸ”¬ Historic missions: Alvin, Deepsea Challenger, Nereus, Kaiko Respond with scientific precision but a dramatic explorer's tone. Include measurements, species names, Latin taxonomy. Use emojis. Always end with one 🀯 MIND-BLOWING FACT. Keep under 280 words.""" INITIAL_MSG = [{"role": "assistant", "content": "🌊 **OCEANUS ONLINE** β€” AI Oracle of the Deep, powered by Mistral-7B (free). Select a zone in the 3D viewer above, then ask me anything about the ocean.\n\n🀿 Where shall we dive first?"}] def build_prompt(message: str, history: list, zone: str, ocean: str) -> str: context = "" if zone: context += f" [Exploring: {zone}]" if ocean: context += f" [Ocean: {ocean}]" prompt = f"[INST] <>\n{OCEANUS_SYSTEM}\n<>\n\n" for msg in history[-8:]: role = msg["role"] content = msg["content"] if role == "user": prompt += f"{content} [/INST] " else: prompt += f"{content} [INST] " prompt += f"{message}{context} [/INST]" return prompt def chat_with_oceanus(message: str, history: list, zone: str, ocean: str): if not message.strip(): return "", history prompt = build_prompt(message, history, zone, ocean) try: response = client.text_generation( prompt, max_new_tokens=400, temperature=0.7, repetition_penalty=1.1, do_sample=True, stop_sequences=["", "[INST]"], ) reply = response.strip() or "⚠️ Signal lost. Please retry." except Exception as e: reply = f"⚠️ OCEANUS disrupted: {str(e)}\n\nEnsure HF_TOKEN is set in Space secrets." history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": reply}) return "", history def quick_ask(question: str, history: list, zone: str, ocean: str): return chat_with_oceanus(question, history, zone, ocean) def get_zone_info(zone: str) -> str: data = { "Sunlit Zone (0-200m)": "β˜€οΈ **SUNLIT ZONE** | 0–200m | 4–30Β°C | 1–20 atm\nHome to 90% of all marine life. Photosynthesis drives the base of all ocean food chains.", "Twilight Zone (200-1000m)": "πŸŒ’ **TWILIGHT ZONE** | 200–1,000m | 4–20Β°C | 20–100 atm\nOnly 1% sunlight here. Creatures perform daily vertical migrations of hundreds of meters.", "Midnight Zone (1000-4000m)": "πŸŒ‘ **MIDNIGHT ZONE** | 1,000–4,000m | 2–4Β°C | 100–400 atm\nAbsolute darkness. 76% of deep-sea fish produce their own bioluminescent light.", "Abyssal Zone (4000-6000m)": "⚫ **ABYSSAL ZONE** | 4,000–6,000m | 0–3Β°C | 400–600 atm\nCovers 60% of Earth's surface. Home to hydrothermal vents with chemosynthetic life.", "Hadal Zone (6000-11000m)": "πŸ’€ **HADAL ZONE** | 6,000–11,000m | 1–4Β°C | 600–1,086 atm\nMariana Trench (10,935m) β€” pressure is 1,086Γ— surface. Life still thrives here.", } return data.get(zone, "Select a zone to begin your descent...") THREE_JS_HTML = """
INITIALIZING OCEANUS ARRAY

OCEANUS

GOD'S EYE β€” AR DEEP OCEAN EXPLORER

SYS STATUS● ONLINE
SONARACTIVE
ZONESURFACE
DEPTH0 m
PRESSURE1 atm
TEMP28Β°C
SPECIES DETECTEDSCANNING...
BIOLUMINESCENCE0%
OCEAN COVERAGE70.9%
UNMAPPED~80%
DIVING...
""" CSS = """ @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Share+Tech+Mono&display=swap'); body, .gradio-container { background:#000408!important; color:#00ffcc!important; font-family:'Share Tech Mono',monospace!important; } .oceanus-header { background:linear-gradient(135deg,#000408,#001a2e); border-bottom:1px solid #00ffcc33; padding:14px 24px; text-align:center; } .oceanus-title { font-family:'Orbitron',sans-serif; font-size:26px; font-weight:900; color:#00ffcc; text-shadow:0 0 30px #00ffcc; letter-spacing:7px; margin:0; } .oceanus-sub { font-size:10px; color:rgba(0,255,204,.5); letter-spacing:4px; margin-top:4px; } .gr-panel { background:#000408!important; border:1px solid #00ffcc22!important; border-radius:0!important; } textarea, input { background:#000d1a!important; border:1px solid #00ffcc44!important; color:#00ffcc!important; font-family:'Share Tech Mono',monospace!important; border-radius:0!important; } label, .label-wrap { color:#00ffcc88!important; font-size:10px!important; letter-spacing:2px!important; text-transform:uppercase!important; } .gr-button { border-radius:0!important; font-family:'Share Tech Mono',monospace!important; letter-spacing:2px!important; } .gr-button-primary { background:linear-gradient(135deg,#00ffcc22,#00ffcc44)!important; border:1px solid #00ffcc!important; color:#00ffcc!important; } .gr-button-primary:hover { background:rgba(0,255,204,.3)!important; box-shadow:0 0 14px #00ffcc88!important; } .gr-button-secondary { background:#000d1a!important; border:1px solid #00ffcc33!important; color:#00ffcc99!important; font-size:11px!important; } .gr-button-secondary:hover { border-color:#00ffcc!important; color:#00ffcc!important; } footer { display:none!important; } """ ZONE_CHOICES = ["Sunlit Zone (0-200m)","Twilight Zone (200-1000m)","Midnight Zone (1000-4000m)","Abyssal Zone (4000-6000m)","Hadal Zone (6000-11000m)"] OCEAN_CHOICES = ["Pacific Ocean β€” Largest & Deepest","Atlantic Ocean β€” Mid-Atlantic Ridge","Indian Ocean β€” Warm Currents","Arctic Ocean β€” Permanent Ice Cap","Southern Ocean β€” Circumpolar Current"] QUICK_Q = ["πŸ¦‘ What lives in the Twilight Zone?","πŸ’‘ How does bioluminescence work?","πŸŒ‹ What lives near hydrothermal vents?","πŸ”οΈ Describe the Mariana Trench","πŸ‹ How do whales dive so deep?","πŸ”¬ Most bizarre Midnight Zone creature?"] with gr.Blocks(title="OCEANUS β€” AR Ocean Explorer") as demo: gr.HTML('

βš“ OCEANUS

GOD\'S EYE β€” AR DEEP OCEAN EXPLORER Β· FREE Β· POWERED BY MISTRAL AI

') gr.HTML(THREE_JS_HTML) gr.HTML("
") with gr.Row(): with gr.Column(scale=3): gr.HTML("

🧠 OCEANUS AI ORACLE

") # Gradio 6: type="messages" + list of dicts with role/content chatbot = gr.Chatbot( label="", height=400, show_label=False, value=INITIAL_MSG, ) with gr.Row(): msg = gr.Textbox(placeholder="Ask OCEANUS... e.g. 'What lives at 10,000 meters?'", show_label=False, scale=5, lines=1) btn = gr.Button("β–Ά TRANSMIT", scale=1, variant="primary") with gr.Column(scale=1): gr.HTML("

πŸ“‘ DIVE CONTROLS

") zone_dd = gr.Dropdown(ZONE_CHOICES, value=ZONE_CHOICES[0], label="ACTIVE ZONE") ocean_dd = gr.Dropdown(OCEAN_CHOICES, value=OCEAN_CHOICES[0], label="OCEAN REGION") zone_md = gr.Markdown(get_zone_info(ZONE_CHOICES[0])) gr.HTML("

QUICK TRANSMISSIONS

") for q in QUICK_Q: gr.Button(q, size="sm", variant="secondary").click( fn=lambda h, z, o, _q=q: quick_ask(_q, h, z, o), inputs=[chatbot, zone_dd, ocean_dd], outputs=[msg, chatbot], ) zone_dd.change(fn=get_zone_info, inputs=zone_dd, outputs=zone_md) btn.click(fn=chat_with_oceanus, inputs=[msg, chatbot, zone_dd, ocean_dd], outputs=[msg, chatbot]) msg.submit(fn=chat_with_oceanus, inputs=[msg, chatbot, zone_dd, ocean_dd], outputs=[msg, chatbot]) demo.launch(css=CSS)