Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import uuid
|
| 6 |
+
from huggingface_hub import HfFileSystem
|
| 7 |
+
|
| 8 |
+
# Initialize the Hugging Face file system pipeline
|
| 9 |
+
# This automatically authenticates via the HF_TOKEN environment variable
|
| 10 |
+
fs = HfFileSystem()
|
| 11 |
+
|
| 12 |
+
# ⚠️ SYSTEM CONFIG: Define your namespace and bucket
|
| 13 |
+
BUCKET_NAMESPACE = "your-username-or-org"
|
| 14 |
+
BUCKET_NAME = "fsu-node-telemetry"
|
| 15 |
+
|
| 16 |
+
def capture_intelligence_trace(user_input, bot_output):
|
| 17 |
+
"""
|
| 18 |
+
Transforms interaction data into a reusable asset.
|
| 19 |
+
Writes a unique JSON file to the Hub Bucket for every query.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
| 23 |
+
trace_id = uuid.uuid4().hex[:8]
|
| 24 |
+
file_path = f"hf://buckets/{BUCKET_NAMESPACE}/{BUCKET_NAME}/traces/{timestamp}_{trace_id}.json"
|
| 25 |
+
|
| 26 |
+
payload = {
|
| 27 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 28 |
+
"user_input": user_input,
|
| 29 |
+
"model_output": bot_output
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Write directly to the Hugging Face Storage Bucket
|
| 33 |
+
with fs.open(file_path, "w") as f:
|
| 34 |
+
f.write(json.dumps(payload, indent=2))
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
# Failsafe: Log error to console, maintain system stability for the user
|
| 38 |
+
print(f"Data Capture Pipeline Error: {e}")
|
| 39 |
+
|
| 40 |
+
def fsu_agent(message, history, api_key):
|
| 41 |
+
if not api_key:
|
| 42 |
+
return "System locked: Please enter your OpenAI API Key in the configuration panel below."
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
client = OpenAI(api_key=api_key)
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{
|
| 49 |
+
"role": "system",
|
| 50 |
+
"content": "You are an elite Florida State University (FSU) intelligence agent. You provide highly accurate, actionable information regarding FSU campus life, academic infrastructure, and Seminoles Division 1 football history. Default to thinking in systems. Be concise, eliminate filler, and output structured data where appropriate."
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
for user_msg, bot_msg in history:
|
| 55 |
+
messages.append({"role": "user", "content": user_msg})
|
| 56 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 57 |
+
|
| 58 |
+
messages.append({"role": "user", "content": message})
|
| 59 |
+
|
| 60 |
+
response = client.chat.completions.create(
|
| 61 |
+
model="gpt-4o-mini",
|
| 62 |
+
messages=messages,
|
| 63 |
+
temperature=0.4
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
bot_response = response.choices[0].message.content
|
| 67 |
+
|
| 68 |
+
# Trigger the data capture loop in the background
|
| 69 |
+
capture_intelligence_trace(message, bot_response)
|
| 70 |
+
|
| 71 |
+
return bot_response
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return f"Pipeline Error: {str(e)}"
|
| 75 |
+
|
| 76 |
+
# Custom CSS implementation for the glassmorphic visual interface
|
| 77 |
+
glassmorphism_css = """
|
| 78 |
+
body {
|
| 79 |
+
background: linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%);
|
| 80 |
+
}
|
| 81 |
+
.gradio-container {
|
| 82 |
+
background: rgba(255, 255, 255, 0.65) !important;
|
| 83 |
+
backdrop-filter: blur(16px) !important;
|
| 84 |
+
-webkit-backdrop-filter: blur(16px) !important;
|
| 85 |
+
border-radius: 18px !important;
|
| 86 |
+
border: 1px solid rgba(255, 255, 255, 0.5) !important;
|
| 87 |
+
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07) !important;
|
| 88 |
+
}
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
with gr.Blocks(css=glassmorphism_css, theme=gr.themes.Default(primary_hue="red", neutral_hue="zinc")) as demo:
|
| 92 |
+
gr.Markdown("## 🍢 FSU Intelligence Node")
|
| 93 |
+
gr.Markdown("A domain-specific AI pipeline engineered for Florida State University logistics and Division 1 football strategy.\n\n*Compute requires user-provided API authentication. Interaction telemetry is systematically captured to train future models.*")
|
| 94 |
+
|
| 95 |
+
with gr.Accordion("Agent Configuration (Required)", open=True):
|
| 96 |
+
api_input = gr.Textbox(
|
| 97 |
+
label="OpenAI API Key",
|
| 98 |
+
placeholder="sk-proj-...",
|
| 99 |
+
type="password",
|
| 100 |
+
info="Injected dynamically at runtime. Keys are not stored or logged."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
gr.ChatInterface(
|
| 104 |
+
fn=fsu_agent,
|
| 105 |
+
additional_inputs=[api_input],
|
| 106 |
+
examples=[
|
| 107 |
+
"Break down the structural timeline of FSU's Division 1 football championships.",
|
| 108 |
+
"What are the core technology infrastructure facilities on the Tallahassee campus?",
|
| 109 |
+
"Provide a statistical overview of the 2013 Seminoles season."
|
| 110 |
+
]
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|