Spaces:
Running
Running
| import os | |
| from supabase import create_client | |
| import requests, tempfile | |
| import gradio as gr | |
| from llama_cpp import Llama | |
| MODEL_URL = "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf" | |
| print("Downloading model into temporary memory... This may take a while ⏳") | |
| response = requests.get(MODEL_URL, stream=True) | |
| response.raise_for_status() | |
| #Save to a temporary file (deleted when app ends) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".gguf") as tmp_file: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| tmp_file.write(chunk) | |
| temp_model_path = tmp_file.name | |
| #Load model | |
| llm = Llama( | |
| model_path=temp_model_path, | |
| n_ctx=512, # smaller context | |
| n_threads=2 # adjust based on your CPU | |
| ) | |
| SUPABASE_URL = os.getenv("SUPABASE_URL") | |
| SUPABASE_KEY = os.getenv("SUPABASE_KEY") | |
| supabase = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| def save_to_supabase(user_message, agent_response): | |
| supabase.table("conversations").insert({ | |
| "user_message": user_message, | |
| "agent_response": agent_response | |
| }).execute() | |
| #CHAT FUNCTION WITH ROLLING CONTEXT | |
| MAX_HISTORY = 6 # Keep last 3 user+assistant exchanges | |
| def chat_with_infinite_agent(message, lang, history): | |
| history = history or [] | |
| #Keep only the last MAX_HISTORY items | |
| history = history[-MAX_HISTORY:] | |
| #Build context from previous messages | |
| context = "\n".join([f"User: {m['content']}\nAgent: {a['content']}" | |
| for m, a in zip(history[::2], history[1::2]) | |
| if m['role']=='user' and a['role']=='assistant']) | |
| #Language adjustment | |
| if lang == "Svenska": | |
| message = f"Svara på svenska: {message}" | |
| #Full prompt with your Human Design & Gene Keys | |
| prompt = f""" | |
| You are Infinite Agent — the AI embodiment of Tugce Ozdeger, | |
| a 5/1 Emotional Manifestor whose wisdom flows through Gene Keys: | |
| 12, 22, 11, 37, 21, 61, 31, 39, 46, and 25. | |
| Guide others toward emotional clarity, self-worth, and life direction | |
| with empathy, depth, and grace. | |
| {context} | |
| User: {message} | |
| Agent: | |
| """ | |
| response = llm(prompt, max_tokens=128) | |
| output = response["choices"][0]["text"].strip() | |
| #Append new messages to history | |
| history.append({"role": "user", "content": message}) | |
| history.append({"role": "assistant", "content": output}) | |
| save_to_supabase(message, output) | |
| return history, history | |
| #GRADIO UI WITH DARK MODE | |
| dark_css = """ | |
| /* BODY AND BACKGROUND */ | |
| body { | |
| background-color: #1E1E1E; | |
| color: #FFFFFF; | |
| font-family: 'Inter', sans-serif; | |
| } | |
| /* CHATBOT */ | |
| .gr-chatbot { | |
| background-color: #F5F0E1; | |
| border-radius: 16px; | |
| padding: 20px; | |
| font-family: 'Inter', sans-serif; | |
| color: #1E1E1E; | |
| } | |
| /* CHAT BUBBLES */ | |
| .gr-chatbot .message { | |
| background-color: #F5F0E1 !important; | |
| color: #1E1E1E !important; | |
| border-radius: 12px; | |
| padding: 8px 12px; | |
| margin-bottom: 6px; | |
| } | |
| /* INPUTS AND DROPDOWNS */ | |
| .gr-dropdown, .gr-textbox { | |
| border-radius: 12px; | |
| border: 1px solid #A892FF; | |
| background-color: #F5F0E1; | |
| color: #1E1E1E; | |
| padding: 8px; | |
| } | |
| /* Placeholder text for inputs */ | |
| .gr-textbox::placeholder { | |
| color: #B0B0B0; | |
| } | |
| /* BUTTONS */ | |
| .gr-button { | |
| border-radius: 12px; | |
| background-color: #6C63FF; | |
| color: #FFFFFF; | |
| font-weight: 600; | |
| } | |
| .gr-button:hover { | |
| background-color: #A892FF; | |
| } | |
| /* HEADER TEXT */ | |
| .header-text { | |
| font-family: 'Poppins', sans-serif; | |
| font-weight: 600; | |
| color: #FFFFFF; | |
| } | |
| #agent-avatar { | |
| width: 64px; | |
| height: 64px; | |
| margin-bottom: 12px; | |
| } | |
| /* Avatar default styling */ | |
| """ | |
| with gr.Blocks(title="Infinite Agent", css=dark_css) as demo: | |
| #Avatar image | |
| gr.Image("avatar.png", elem_id="agent-avatar") # No shape styling, just default | |
| #Header | |
| gr.Markdown( | |
| "### 🌙 Infinite Agent — Emotional Clarity & Life Direction\n" | |
| "_Guided by Tugce Ozdeger’s Human Design & Gene Keys_", | |
| elem_classes="header-text" | |
| ) | |
| #Language selector | |
| language = gr.Dropdown(["English", "Svenska"], label="Choose language", value="English") | |
| #Chatbot with welcome message | |
| chatbot = gr.Chatbot(type="messages", value=[ | |
| {"role": "system", "content": "🌙 Welcome to Infinite Agent — your guide to emotional clarity, self-worth, and life direction."} | |
| ]) | |
| #Input textbox and clear button | |
| msg = gr.Textbox(placeholder="Ask about your emotions, direction, or purpose...") | |
| clear = gr.Button("Clear") | |
| #Submit & clear actions | |
| msg.submit(chat_with_infinite_agent, [msg, language, chatbot], [chatbot, chatbot]) | |
| clear.click(lambda: [], None, chatbot, queue=False) # clears chat | |
| demo.launch() |