File size: 4,277 Bytes
ff2f68e
 
 
0f98303
af14ecd
 
 
 
0f98303
ff2f68e
03ff466
 
0f98303
 
ff2f68e
 
 
0f98303
03ff466
ff2f68e
 
0f98303
ff2f68e
 
 
af14ecd
03ff466
 
ff2f68e
0f98303
ff2f68e
 
03ff466
0f98303
ff2f68e
0f98303
 
 
af14ecd
03ff466
 
 
0f98303
ff2f68e
 
03ff466
 
0f98303
ff2f68e
0f98303
ff2f68e
 
 
 
 
 
 
 
 
 
 
 
 
 
c596379
ff2f68e
c596379
ff2f68e
0f98303
 
 
 
 
 
 
 
 
af14ecd
 
0f98303
 
 
 
 
 
 
 
 
 
 
 
ff2f68e
0f98303
 
 
 
 
ff2f68e
0f98303
 
 
 
ff2f68e
 
c596379
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import gradio as gr
from openai import OpenAI

def respond(message, chat_history, api_key):
    # Failsafe to ensure history initializes as an empty list
    if chat_history is None:
        chat_history = []
        
    # Gatekeeper: Halt execution if no compute resource is provided
    if not api_key:
        chat_history.append({"role": "user", "content": message})
        chat_history.append({"role": "assistant", "content": "System locked: Please enter your OpenAI API Key in the secure box above."})
        return "", chat_history
        
    try:
        client = OpenAI(api_key=api_key)
        
        # System prompt
        openai_messages = [
            {
                "role": "system", 
                "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."
            }
        ]
        
        # Pass the direct Gradio history into the OpenAI pipeline
        openai_messages.extend(chat_history)
        openai_messages.append({"role": "user", "content": message})
        
        # Execute query
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=openai_messages,
            temperature=0.4
        )
        
        bot_reply = response.choices[0].message.content
        
        # Append the new interaction using the native dictionary format
        chat_history.append({"role": "user", "content": message})
        chat_history.append({"role": "assistant", "content": bot_reply})
        
        return "", chat_history
        
    except Exception as e:
        chat_history.append({"role": "user", "content": message})
        chat_history.append({"role": "assistant", "content": f"Pipeline Error: {str(e)}"})
        return "", chat_history

# Custom CSS for the glassmorphic visual interface
glassmorphism_css = """
body {
    background: linear-gradient(135deg, #fdfbfb 0%, #ebedee 100%);
}
.gradio-container {
    background: rgba(255, 255, 255, 0.65) !important;
    backdrop-filter: blur(16px) !important;
    -webkit-backdrop-filter: blur(16px) !important;
    border-radius: 18px !important;
    border: 1px solid rgba(255, 255, 255, 0.5) !important;
    box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07) !important;
}
"""

with gr.Blocks() as demo:
    gr.Markdown("## 🍢 FSU Intelligence Node")
    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.*")
    
    # 1. API Key Input - Isolated from the chat submission wipe
    with gr.Row():
        api_input = gr.Textbox(
            label="OpenAI API Key (Required)", 
            placeholder="sk-proj-...", 
            type="password",
            info="Injected dynamically at runtime. Keys are not stored or logged."
        )
        
    # 2. Main Chat Display - Removed the 'type' parameter as it is now strictly implicit
    chatbot = gr.Chatbot(height=450, label="Intelligence Feed")
    
    # 3. User Input Area
    with gr.Row():
        msg = gr.Textbox(
            label="Query Input",
            placeholder="Type your query here and press Enter...",
            scale=4
        )
        submit_btn = gr.Button("Send", variant="primary", scale=1)
        
    # 4. Quick-Load Examples
    gr.Examples(
        examples=[
            "Break down the structural timeline of FSU's Division 1 football championships.",
            "What are the core technology infrastructure facilities on the Tallahassee campus?",
            "Provide a statistical overview of the 2013 Seminoles season."
        ],
        inputs=msg
    )
        
    # 5. Explicit Event Binding
    msg.submit(respond, inputs=[msg, chatbot, api_input], outputs=[msg, chatbot])
    submit_btn.click(respond, inputs=[msg, chatbot, api_input], outputs=[msg, chatbot])

if __name__ == "__main__":
    demo.launch(
        css=glassmorphism_css, 
        theme=gr.themes.Default(primary_hue="red", neutral_hue="zinc")
    )