exp-chat-rag / app.py
Arnic's picture
revert: simple lazy loading inside @spaces.GPU — working baseline
7346617
Raw
History Blame Contribute Delete
4.12 kB
"""Gradio Chat UI for Aethron Portfolio Agent — ZeroGPU-compatible."""
import spaces # noqa: E402 — MUST BE FIRST
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
import gradio as gr
print("=" * 55)
print("AETHRON PORTFOLIO AGENT — GPU Mode (ZeroGPU)")
print("=" * 55)
# Pipeline loaded lazily inside @spaces.GPU context — avoids CUDA init in main process
_pipeline = None
def get_pipeline():
global _pipeline
if _pipeline is None:
from rag_pipeline import AethronPipeline
print("Loading pipeline (first query)...")
_pipeline = AethronPipeline(
build_index=not os.path.exists("data/index/faiss.index")
)
return _pipeline
@spaces.GPU(duration=60)
def respond(message, history):
"""Handle chat — runs inside GPU context so torch.cuda patches apply."""
if not message or not message.strip():
return history
pipeline = get_pipeline()
result = pipeline.query(message.strip())
response = result["answer"]
if result["sources"]:
source_names = [s.replace("_", " ").title() for s in result["sources"][:5]]
response += "\n\n**Sources:** " + " | ".join(source_names)
history.append({"role": "user", "content": message})
history.append({"role": "assistant", "content": response})
return history
CUSTOM_CSS = """
.container { max-width: 900px; margin: auto; }
.header { text-align: center; padding: 20px; }
.header h1 { color: #1a1a2e; font-family: 'Segoe UI', sans-serif; }
.header p { color: #4a4a6a; }
"""
with gr.Blocks(title="Aethron | Chat with Arash's Portfolio") as demo:
gr.HTML("""
<div class="header">
<h1>Aethron</h1>
<p><strong>Neuro-Symbolic Portfolio Agent</strong> — Ask about Arash Nicoomanesh's
experience, architecture, and projects</p>
<p style="font-size: 0.9em; color: #888;">
<a href="https://aragit.github.io" target="_blank">Portfolio</a> |
<a href="https://github.com/aragit" target="_blank">GitHub</a> |
<a href="https://kaggle.com/arashnic" target="_blank">Kaggle</a> |
<a href="https://huggingface.co/Arnic" target="_blank">HuggingFace</a>
</p>
</div>
""")
chatbot = gr.Chatbot(height=500)
with gr.Row():
msg_input = gr.Textbox(
placeholder="Ask about skills, experience, AXIOMIS architecture, or projects...",
scale=8,
show_label=False,
autofocus=True,
)
send_btn = gr.Button("Send", scale=1, variant="primary")
with gr.Row():
gr.Examples(
examples=[
"What is Arash's experience with neuro-symbolic systems?",
"Does he have Kubernetes and cloud deployment skills?",
"Explain the Nash Marketing Agents project",
"What is the Composable Intelligence Stack?",
"How many years of experience does Arash have?",
"What publications has he written?",
"Tell me about the Barnabus project",
"Is Arash available for remote work?",
"Where has Arash worked?",
"What are his core technical skills?",
"What energy projects has he built?",
"What are his Kaggle achievements?",
],
inputs=msg_input,
label="Example Questions",
)
clear_btn = gr.Button("Clear Conversation", variant="secondary")
msg_input.submit(respond, [msg_input, chatbot], [chatbot])
send_btn.click(respond, [msg_input, chatbot], [chatbot])
clear_btn.click(lambda: [], None, [chatbot], queue=False)
gr.Markdown("""
---
<p style="text-align: center; color: #888; font-size: 0.85em;">
Powered by Type 2 Neuro-Symbolic RAG |
Phi-3.5-mini + BGE-small + FAISS |
Running on Hugging Face Spaces
</p>
""")
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True,
css=CUSTOM_CSS,
)