import os from supabase import create_client import requests, tempfile import gradio as gr from llama_cpp import Llama #MODEL DOWNLOAD & LOAD MODEL_URL = "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf" #MODEL_URL = "https://huggingface.co/TheBloke/bling-stable-lm-3b-4e1t-v0-GGUF/resolve/main/bling-stable-lm-3b-4e1t-v0.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 SETUP SUPABASE_URL = os.getenv("SUPABASE_URL") SUPABASE_KEY = os.getenv("SUPABASE_KEY") supabase = create_client(SUPABASE_URL, SUPABASE_KEY) def save_to_supabase(user_id, prompt, response, model, tokens=None): try: supabase.table("ai_logs").insert({ "user_id": user_id, "prompt": prompt, "response": response, "model": model, "tokens": tokens }).execute() except Exception as e: print("Supabase insert failed:", e) #CHAT FUNCTION WITH ROLLING CONTEXT MAX_HISTORY = 4 # Keep last 2 user+assistant exchanges for stability 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 (trimmed for stability) context_lines = [] for m, a in zip(history[::2], history[1::2]): if m['role']=='user' and a['role']=='assistant': #Trim very long messages to reduce memory user_msg = m['content'][:300] assistant_msg = a['content'][:300] context_lines.append(f"User: {user_msg}\nAgent: {assistant_msg}") context = "\n".join(context_lines) #Language adjustment if lang == "Svenska": message = f"Svara på svenska: {message}" elif lang == "Türkçe": message = f"Türkçe cevapla: {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: """ #Generate response safely try: response = llm(prompt, max_tokens=512) output = response["choices"][0]["text"].strip() except Exception as e: print("LLM error:", e) output = "⚠️ Sorry, the model failed to respond. Please try again." #Append new messages to history history.append({"role": "user", "content": message}) history.append({"role": "assistant", "content": output}) save_to_supabase( user_id="anonymous", prompt=message, response=output, model=MODEL_URL, tokens=len(message) ) return history, history #GRADIO UI WITH DARK MODE css_path = os.path.join(os.path.dirname(__file__), "style.css") with gr.Blocks(title="Infinite Agent", css=open(css_path).read()) 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", "Türkçe"], 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()