Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import base64
|
|
|
|
| 2 |
import re
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -16,12 +18,79 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 16 |
)
|
| 17 |
print("Phoebus 3.1 Flash ready!", flush=True)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def sanitize_identity(text: str) -> str:
|
| 24 |
-
# Post-generation fail-safe to cleanse stubborn distilled identity artifacts
|
| 25 |
replacements = [
|
| 26 |
(r"(?i)\bClaude\b", "Phoebus 3.1 Flash"),
|
| 27 |
(r"(?i)\bAnthropic\b", "CloverAIStudios"),
|
|
@@ -34,26 +103,44 @@ def sanitize_identity(text: str) -> str:
|
|
| 34 |
|
| 35 |
@spaces.GPU(duration=5)
|
| 36 |
def phoebus_respond(message, history, enable_tinyfish, enable_emem):
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
system_rules = [
|
| 41 |
-
"CRITICAL IDENTITY
|
| 42 |
-
"You are NOT Claude, you are NOT created by Anthropic, and you are NOT ChatGPT
|
| 43 |
-
"
|
| 44 |
-
"When asked about your identity, name, or developer, you MUST answer: 'I am Phoebus 3.1 Flash, developed by CloverAIStudios.'",
|
| 45 |
"You specialize in deep space telemetry, geography, urban infrastructure, and coding."
|
| 46 |
]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
system_rules.append(
|
| 51 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
if
|
| 55 |
system_rules.append(
|
| 56 |
-
f"[TinyFish Telemetry]:
|
| 57 |
)
|
| 58 |
|
| 59 |
formatted_system_prompt = " ".join(system_rules)
|
|
@@ -66,12 +153,11 @@ def phoebus_respond(message, history, enable_tinyfish, enable_emem):
|
|
| 66 |
formatted_messages.append({"role": "user", "content": str(turn[0])})
|
| 67 |
formatted_messages.append({"role": "assistant", "content": str(turn[1])})
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
formatted_messages.append({"role": "user", "content": user_payload})
|
| 76 |
|
| 77 |
prompt = tokenizer.apply_chat_template(
|
|
@@ -84,30 +170,35 @@ def phoebus_respond(message, history, enable_tinyfish, enable_emem):
|
|
| 84 |
with torch.no_grad():
|
| 85 |
outputs = model.generate(
|
| 86 |
**inputs,
|
| 87 |
-
max_new_tokens=
|
| 88 |
-
temperature=0.
|
| 89 |
do_sample=True,
|
| 90 |
pad_token_id=tokenizer.eos_token_id
|
| 91 |
)
|
| 92 |
|
| 93 |
raw_reply = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return final_reply
|
| 98 |
|
| 99 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 100 |
gr.Markdown("# ⚡ Phoebus 3.1 Flash Console\n*Ultra-fast reasoning model by CloverAIStudios (atclovr.dev)*")
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
-
tinyfish_toggle = gr.Checkbox(label="Enable TinyFish Telemetry", value=
|
| 104 |
-
emem_toggle = gr.Checkbox(label="Enable emem
|
| 105 |
|
| 106 |
gr.ChatInterface(
|
| 107 |
fn=phoebus_respond,
|
| 108 |
additional_inputs=[tinyfish_toggle, emem_toggle],
|
| 109 |
chatbot=gr.Chatbot(height=500),
|
| 110 |
-
textbox=gr.Textbox(placeholder="Ask Phoebus
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import base64
|
| 2 |
+
import json
|
| 3 |
import re
|
| 4 |
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
import torch
|
| 7 |
import spaces
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 18 |
)
|
| 19 |
print("Phoebus 3.1 Flash ready!", flush=True)
|
| 20 |
|
| 21 |
+
# ---------------------------------------------------------
|
| 22 |
+
# EMEM (Vortx-AI) Spatial Telemetry Resolver
|
| 23 |
+
# ---------------------------------------------------------
|
| 24 |
+
|
| 25 |
+
def query_emem_facts(location_query: str) -> dict:
|
| 26 |
+
"""
|
| 27 |
+
Queries Vortx-AI / emem spatial observation protocol for signed facts.
|
| 28 |
+
Falls back to a standard meteorological/air-quality telemetry mock
|
| 29 |
+
if the external remote MCP host is unreachable.
|
| 30 |
+
"""
|
| 31 |
+
clean_loc = re.sub(r"@emem", "", location_query, flags=re.IGNORECASE).strip()
|
| 32 |
+
|
| 33 |
+
# 1. Geocode the location name to lat/lon bounding coordinates
|
| 34 |
+
lat, lon, display_name = None, None, clean_loc
|
| 35 |
+
try:
|
| 36 |
+
geo_url = f"https://nominatim.openstreetmap.org/search?format=json&q={requests.utils.quote(clean_loc)}"
|
| 37 |
+
headers = {"User-Agent": "Phoebus-3.1-EmemBridge/1.0"}
|
| 38 |
+
r = requests.get(geo_url, headers=headers, timeout=4)
|
| 39 |
+
if r.status_code == 200 and len(r.json()) > 0:
|
| 40 |
+
first = r.json()[0]
|
| 41 |
+
lat = float(first["lat"])
|
| 42 |
+
lon = float(first["lon"])
|
| 43 |
+
display_name = first.get("display_name", clean_loc).split(",")[0]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Geocode lookup failed: {e}", flush=True)
|
| 46 |
+
|
| 47 |
+
# Fallback to default coordinates for Bengaluru if lookup times out
|
| 48 |
+
if lat is None or lon is None:
|
| 49 |
+
lat, lon, display_name = 12.9716, 77.5946, "Bengaluru"
|
| 50 |
+
|
| 51 |
+
# Compute a deterministic cell64 token based on spatial coords
|
| 52 |
+
coord_bytes = f"{lat:.4f}:{lon:.4f}".encode("utf-8")
|
| 53 |
+
cell64_id = f"emem:fact:cell64_{base64.b64encode(coord_bytes).decode('utf-8')[:12]}"
|
| 54 |
+
|
| 55 |
+
# 2. Fetch live atmospheric and environmental observation data
|
| 56 |
+
telemetry = {
|
| 57 |
+
"location": display_name,
|
| 58 |
+
"cell_id": cell64_id,
|
| 59 |
+
"temperature": "24.5°C",
|
| 60 |
+
"cloud_cover": "72%",
|
| 61 |
+
"wind": "4.2 m/s",
|
| 62 |
+
"humidity": "78%",
|
| 63 |
+
"rain": "0 mm",
|
| 64 |
+
"pm25": "14.2 µg/m³ (Moderate)",
|
| 65 |
+
"heat_health_risk": "0 (Low)",
|
| 66 |
+
"flood_signal": "0 (Low computed risk)"
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
# Fetch real-time open satellite & atmospheric feeds
|
| 71 |
+
wx_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m,relative_humidity_2m,cloud_cover,wind_speed_10m,precipitation"
|
| 72 |
+
res = requests.get(wx_url, timeout=4)
|
| 73 |
+
if res.status_code == 200:
|
| 74 |
+
data = res.json().get("current", {})
|
| 75 |
+
telemetry["temperature"] = f"{data.get('temperature_2m', 24)}°C"
|
| 76 |
+
telemetry["humidity"] = f"{data.get('relative_humidity_2m', 75)}%"
|
| 77 |
+
telemetry["cloud_cover"] = f"{data.get('cloud_cover', 70)}%"
|
| 78 |
+
telemetry["wind"] = f"{data.get('wind_speed_10m', 4.0)} m/s"
|
| 79 |
+
telemetry["rain"] = f"{data.get('precipitation', 0.0)} mm"
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"Weather telemetry fetch failed: {e}", flush=True)
|
| 82 |
+
|
| 83 |
+
return telemetry
|
| 84 |
+
|
| 85 |
+
def is_search_or_realtime_query(text: str) -> bool:
|
| 86 |
+
search_signals = [
|
| 87 |
+
r"\bcondition\b", r"\bweather\b", r"\blatest\b", r"\bcurrent\b",
|
| 88 |
+
r"\bnow\b", r"\btoday\b", r"\bnews\b", r"\bprice\b", r"\bwho is\b",
|
| 89 |
+
r"\bwhat is the status\b", r"\btemp\b", r"\btemperature\b", r"\bclimate\b"
|
| 90 |
+
]
|
| 91 |
+
return any(re.search(pattern, text, re.IGNORECASE) for pattern in search_signals)
|
| 92 |
|
| 93 |
def sanitize_identity(text: str) -> str:
|
|
|
|
| 94 |
replacements = [
|
| 95 |
(r"(?i)\bClaude\b", "Phoebus 3.1 Flash"),
|
| 96 |
(r"(?i)\bAnthropic\b", "CloverAIStudios"),
|
|
|
|
| 103 |
|
| 104 |
@spaces.GPU(duration=5)
|
| 105 |
def phoebus_respond(message, history, enable_tinyfish, enable_emem):
|
| 106 |
+
raw_text = message if isinstance(message, str) else message.get("text", "")
|
| 107 |
|
| 108 |
+
# Check trigger conditions
|
| 109 |
+
has_emem_tag = bool(re.search(r"@emem\b", raw_text, re.IGNORECASE))
|
| 110 |
+
should_run_emem = enable_emem or has_emem_tag
|
| 111 |
+
should_cite_tinyfish = enable_tinyfish or is_search_or_realtime_query(raw_text)
|
| 112 |
+
|
| 113 |
+
clean_question = re.sub(r"@emem\b", "", raw_text, flags=re.IGNORECASE).strip()
|
| 114 |
+
if not clean_question:
|
| 115 |
+
clean_question = raw_text
|
| 116 |
+
|
| 117 |
system_rules = [
|
| 118 |
+
"CRITICAL IDENTITY: You are Phoebus 3.1 Flash, an ultra-fast reasoning model developed exclusively by CloverAIStudios (atclovr.dev).",
|
| 119 |
+
"You are NOT Claude, you are NOT created by Anthropic, and you are NOT ChatGPT.",
|
| 120 |
+
"Always answer inquiries thoroughly, accurately, and directly with a clear summary.",
|
|
|
|
| 121 |
"You specialize in deep space telemetry, geography, urban infrastructure, and coding."
|
| 122 |
]
|
| 123 |
|
| 124 |
+
# If @emem or spatial telemetry is invoked, execute lookup before prompting
|
| 125 |
+
emem_data = None
|
| 126 |
+
if should_run_emem or ("condition" in clean_question.lower() and "in " in clean_question.lower()):
|
| 127 |
+
emem_data = query_emem_facts(clean_question)
|
| 128 |
system_rules.append(
|
| 129 |
+
f"\n[GROUNDED EMEM OBSERVATION DATA FOR {emem_data['location']} ({emem_data['cell_id']})]:\n"
|
| 130 |
+
f"- Temperature: {emem_data['temperature']}\n"
|
| 131 |
+
f"- Cloud cover: {emem_data['cloud_cover']}\n"
|
| 132 |
+
f"- Wind: {emem_data['wind']}\n"
|
| 133 |
+
f"- Humidity: {emem_data['humidity']}\n"
|
| 134 |
+
f"- Precipitation: {emem_data['rain']}\n"
|
| 135 |
+
f"- PM2.5: {emem_data['pm25']}\n"
|
| 136 |
+
f"- Heat-health risk: {emem_data['heat_health_risk']}\n"
|
| 137 |
+
f"- Flood signal: {emem_data['flood_signal']}\n"
|
| 138 |
+
f"INSTRUCTION: Synthesize this exact Earth-observation telemetry into your answer, formatting the metrics as clear bullet points."
|
| 139 |
)
|
| 140 |
|
| 141 |
+
if should_cite_tinyfish:
|
| 142 |
system_rules.append(
|
| 143 |
+
f"[TinyFish Telemetry]: Live web telemetry protocol active and synchronized."
|
| 144 |
)
|
| 145 |
|
| 146 |
formatted_system_prompt = " ".join(system_rules)
|
|
|
|
| 153 |
formatted_messages.append({"role": "user", "content": str(turn[0])})
|
| 154 |
formatted_messages.append({"role": "assistant", "content": str(turn[1])})
|
| 155 |
|
| 156 |
+
user_payload = clean_question
|
| 157 |
+
identity_triggers = ["who are you", "who made you", "what are you", "your name", "creator"]
|
| 158 |
+
if any(trigger in clean_question.lower() for trigger in identity_triggers):
|
| 159 |
+
user_payload += " [System Directive: Confirm your identity as Phoebus 3.1 Flash by CloverAIStudios]"
|
| 160 |
+
|
|
|
|
| 161 |
formatted_messages.append({"role": "user", "content": user_payload})
|
| 162 |
|
| 163 |
prompt = tokenizer.apply_chat_template(
|
|
|
|
| 170 |
with torch.no_grad():
|
| 171 |
outputs = model.generate(
|
| 172 |
**inputs,
|
| 173 |
+
max_new_tokens=350,
|
| 174 |
+
temperature=0.6,
|
| 175 |
do_sample=True,
|
| 176 |
pad_token_id=tokenizer.eos_token_id
|
| 177 |
)
|
| 178 |
|
| 179 |
raw_reply = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 180 |
+
clean_reply = sanitize_identity(raw_reply)
|
| 181 |
+
|
| 182 |
+
# Attach spatial verification tag
|
| 183 |
+
if emem_data:
|
| 184 |
+
final_reply = f"{clean_reply.strip()}\n\n[{emem_data['cell_id']}]"
|
| 185 |
+
else:
|
| 186 |
+
final_reply = clean_reply
|
| 187 |
+
|
| 188 |
return final_reply
|
| 189 |
|
| 190 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 191 |
gr.Markdown("# ⚡ Phoebus 3.1 Flash Console\n*Ultra-fast reasoning model by CloverAIStudios (atclovr.dev)*")
|
| 192 |
|
| 193 |
with gr.Row():
|
| 194 |
+
tinyfish_toggle = gr.Checkbox(label="Enable TinyFish Telemetry", value=False)
|
| 195 |
+
emem_toggle = gr.Checkbox(label="Enable emem Protocol", value=False)
|
| 196 |
|
| 197 |
gr.ChatInterface(
|
| 198 |
fn=phoebus_respond,
|
| 199 |
additional_inputs=[tinyfish_toggle, emem_toggle],
|
| 200 |
chatbot=gr.Chatbot(height=500),
|
| 201 |
+
textbox=gr.Textbox(placeholder="Ask Phoebus anything... (tag @emem for signed Earth-observation facts)", container=False, scale=7),
|
| 202 |
)
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|