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] <[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 = """
GOD'S EYE β AR DEEP OCEAN EXPLORER
β OCEANUS
GOD\'S EYE β AR DEEP OCEAN EXPLORER Β· FREE Β· POWERED BY MISTRAL AI
π§ 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)