Spaces:
Runtime error
Runtime error
Simplified app.py for HF Spaces
Browse files
app.py
CHANGED
|
@@ -4,30 +4,24 @@ Upload PDF → Chat with AI → Export Code
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
-
import tempfile
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
|
| 10 |
import gradio as gr
|
| 11 |
from agno.agent import Agent
|
| 12 |
from agno.knowledge.embedder.openai import OpenAIEmbedder
|
| 13 |
from agno.knowledge.knowledge import Knowledge
|
| 14 |
from agno.knowledge.reader.pdf_reader import PDFReader
|
| 15 |
-
from agno.vectordb.lancedb import LanceDb
|
| 16 |
from agno.models.neosantara import Neosantara
|
| 17 |
|
| 18 |
-
# Environment
|
| 19 |
NEOSANTARAA_API_KEY = os.getenv("NEOSANTARAA_API_KEY", "")
|
| 20 |
MODEL_ID = os.getenv("MODEL_ID", "grok-4.1-fast")
|
| 21 |
EMBEDDER_ID = os.getenv("EMBEDDER_ID", "nusa-embedding-0001")
|
| 22 |
|
| 23 |
-
# Global state
|
| 24 |
knowledge_base = None
|
| 25 |
agent = None
|
| 26 |
-
|
| 27 |
|
| 28 |
|
| 29 |
def get_embedder():
|
| 30 |
-
"""Get embedder configured for Neosantara."""
|
| 31 |
return OpenAIEmbedder(
|
| 32 |
id=EMBEDDER_ID,
|
| 33 |
api_key=NEOSANTARAA_API_KEY,
|
|
@@ -35,66 +29,50 @@ def get_embedder():
|
|
| 35 |
)
|
| 36 |
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
global knowledge_base, agent, uploaded_file_url
|
| 41 |
|
| 42 |
-
if
|
| 43 |
return "Please upload a PDF file.", None
|
| 44 |
|
| 45 |
-
# Handle Gradio file upload (can be string path or UploadFile object)
|
| 46 |
-
if hasattr(file, "name"):
|
| 47 |
-
# Gradio UploadFile object
|
| 48 |
-
tmp_path = file.name
|
| 49 |
-
elif isinstance(file, str):
|
| 50 |
-
# Already a path
|
| 51 |
-
tmp_path = file
|
| 52 |
-
else:
|
| 53 |
-
# Raw bytes - save to temp
|
| 54 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 55 |
-
tmp.write(file)
|
| 56 |
-
tmp_path = tmp.name
|
| 57 |
-
|
| 58 |
-
# For local files, we'll use file path directly
|
| 59 |
-
# In production, you'd upload to cloud storage
|
| 60 |
-
uploaded_file_url = tmp_path
|
| 61 |
-
|
| 62 |
try:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
embedder = get_embedder()
|
| 65 |
|
| 66 |
-
# Create vector DB
|
| 67 |
vector_db = LanceDb(
|
| 68 |
table_name="rag_demo", uri="/tmp/lancedb", embedder=embedder
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
knowledge.insert(path=tmp_path)
|
| 74 |
|
| 75 |
-
# Create agent
|
| 76 |
agent = Agent(
|
| 77 |
model=Neosantara(
|
| 78 |
id=MODEL_ID,
|
| 79 |
api_key=NEOSANTARAA_API_KEY,
|
| 80 |
base_url="https://api.neosantara.xyz/v1",
|
| 81 |
),
|
| 82 |
-
knowledge=
|
| 83 |
search_knowledge=True,
|
| 84 |
markdown=True,
|
| 85 |
)
|
| 86 |
|
| 87 |
return (
|
| 88 |
-
|
| 89 |
-
|
| 90 |
)
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
return f"❌ Error: {str(e)}", None
|
| 94 |
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
"""Handle chat with RAG agent."""
|
| 98 |
global agent
|
| 99 |
|
| 100 |
if agent is None:
|
|
@@ -107,115 +85,93 @@ def chat(message, history):
|
|
| 107 |
return history + [(message, f"Error: {str(e)}")]
|
| 108 |
|
| 109 |
|
| 110 |
-
def
|
| 111 |
-
|
| 112 |
-
global uploaded_file_url, MODEL_ID, EMBEDDER_ID
|
| 113 |
|
| 114 |
-
if
|
| 115 |
return "Please upload a PDF first!"
|
| 116 |
|
| 117 |
-
|
| 118 |
# Generated from RAG Demo Tool
|
| 119 |
|
| 120 |
from agno.agent import Agent
|
| 121 |
-
from agno.knowledge.
|
| 122 |
-
from agno.
|
| 123 |
-
from agno.
|
|
|
|
| 124 |
from agno.models.neosantara import Neosantara
|
| 125 |
|
| 126 |
-
|
| 127 |
-
NEOSANTARA_API_KEY = "your-api-key-here"
|
| 128 |
MODEL_ID = "{MODEL_ID}"
|
| 129 |
EMBEDDER_ID = "{EMBEDDER_ID}"
|
| 130 |
|
| 131 |
-
# Setup Embedder
|
| 132 |
embedder = OpenAIEmbedder(
|
| 133 |
id=EMBEDDER_ID,
|
| 134 |
-
api_key=
|
| 135 |
base_url="https://api.neosantara.xyz/v1"
|
| 136 |
)
|
| 137 |
|
| 138 |
-
# Setup Vector DB
|
| 139 |
vector_db = LanceDb(
|
| 140 |
table_name="my_doc",
|
| 141 |
uri="/tmp/lancedb",
|
| 142 |
embedder=embedder
|
| 143 |
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
vector_db=vector_db
|
| 149 |
)
|
| 150 |
-
|
| 151 |
|
| 152 |
-
# Create Agent
|
| 153 |
agent = Agent(
|
| 154 |
model=Neosantara(
|
| 155 |
id=MODEL_ID,
|
| 156 |
-
api_key=
|
| 157 |
base_url="https://api.neosantara.xyz/v1"
|
| 158 |
),
|
| 159 |
-
knowledge=
|
| 160 |
search_knowledge=True,
|
| 161 |
markdown=True
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# Chat!
|
| 165 |
agent.print_response("Apa isi dokumen ini?")
|
| 166 |
'''
|
| 167 |
-
return code
|
| 168 |
|
| 169 |
|
| 170 |
-
def
|
| 171 |
-
|
| 172 |
-
global knowledge_base, agent, uploaded_file_url
|
| 173 |
knowledge_base = None
|
| 174 |
agent = None
|
| 175 |
-
|
| 176 |
-
return None, [], "Session cleared.
|
| 177 |
|
| 178 |
|
| 179 |
-
# Build Gradio UI
|
| 180 |
with gr.Blocks(title="Neosantara RAG Demo") as demo:
|
| 181 |
gr.Markdown("# 📄 Neosantara RAG Demo")
|
| 182 |
-
gr.Markdown("Upload a PDF, chat with AI
|
| 183 |
|
| 184 |
with gr.Row():
|
| 185 |
with gr.Column(scale=1):
|
| 186 |
-
file_input = gr.File(
|
| 187 |
-
|
| 188 |
-
)
|
| 189 |
-
upload_btn = gr.Button("Process PDF", variant="primary")
|
| 190 |
-
status_output = gr.Textbox(label="Status", interactive=False)
|
| 191 |
clear_btn = gr.Button("Clear Session")
|
| 192 |
|
| 193 |
with gr.Column(scale=2):
|
| 194 |
chatbot = gr.Chatbot(label="Chat", height=400)
|
| 195 |
-
|
| 196 |
-
label="Message", placeholder="Ask about your document...", scale=3
|
| 197 |
-
)
|
| 198 |
send_btn = gr.Button("Send", variant="primary")
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
export_btn = gr.Button("📋 Export Code")
|
| 202 |
-
|
| 203 |
-
code_output = gr.Code(label="Generated Code", language="python", lines=20)
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
fn=upload_pdf, inputs=[file_input], outputs=[status_output, chatbot]
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
send_btn.click(fn=chat, inputs=[msg_input, chatbot], outputs=[chatbot])
|
| 211 |
-
msg_input.submit(fn=chat, inputs=[msg_input, chatbot], outputs=[chatbot])
|
| 212 |
|
| 213 |
-
export_btn.click(fn=
|
| 214 |
|
| 215 |
-
clear_btn.click(
|
| 216 |
-
fn=clear_session, inputs=[], outputs=[file_input, chatbot, status_output]
|
| 217 |
-
)
|
| 218 |
|
| 219 |
-
# Launch
|
| 220 |
if __name__ == "__main__":
|
| 221 |
-
demo.launch(
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from agno.agent import Agent
|
| 9 |
from agno.knowledge.embedder.openai import OpenAIEmbedder
|
| 10 |
from agno.knowledge.knowledge import Knowledge
|
| 11 |
from agno.knowledge.reader.pdf_reader import PDFReader
|
| 12 |
+
from agno.vectordb.lancedb import LanceDb
|
| 13 |
from agno.models.neosantara import Neosantara
|
| 14 |
|
|
|
|
| 15 |
NEOSANTARAA_API_KEY = os.getenv("NEOSANTARAA_API_KEY", "")
|
| 16 |
MODEL_ID = os.getenv("MODEL_ID", "grok-4.1-fast")
|
| 17 |
EMBEDDER_ID = os.getenv("EMBEDDER_ID", "nusa-embedding-0001")
|
| 18 |
|
|
|
|
| 19 |
knowledge_base = None
|
| 20 |
agent = None
|
| 21 |
+
current_file_path = None
|
| 22 |
|
| 23 |
|
| 24 |
def get_embedder():
|
|
|
|
| 25 |
return OpenAIEmbedder(
|
| 26 |
id=EMBEDDER_ID,
|
| 27 |
api_key=NEOSANTARAA_API_KEY,
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
+
def process_pdf(file_obj):
|
| 33 |
+
global knowledge_base, agent, current_file_path
|
|
|
|
| 34 |
|
| 35 |
+
if file_obj is None:
|
| 36 |
return "Please upload a PDF file.", None
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
+
if hasattr(file_obj, "name"):
|
| 40 |
+
file_path = file_obj.name
|
| 41 |
+
else:
|
| 42 |
+
file_path = str(file_obj)
|
| 43 |
+
|
| 44 |
+
current_file_path = file_path
|
| 45 |
+
|
| 46 |
embedder = get_embedder()
|
| 47 |
|
|
|
|
| 48 |
vector_db = LanceDb(
|
| 49 |
table_name="rag_demo", uri="/tmp/lancedb", embedder=embedder
|
| 50 |
)
|
| 51 |
|
| 52 |
+
knowledge_base = Knowledge(vector_db=vector_db, reader=PDFReader(chunk=True))
|
| 53 |
+
knowledge_base.insert(path=file_path)
|
|
|
|
| 54 |
|
|
|
|
| 55 |
agent = Agent(
|
| 56 |
model=Neosantara(
|
| 57 |
id=MODEL_ID,
|
| 58 |
api_key=NEOSANTARAA_API_KEY,
|
| 59 |
base_url="https://api.neosantara.xyz/v1",
|
| 60 |
),
|
| 61 |
+
knowledge=knowledge_base,
|
| 62 |
search_knowledge=True,
|
| 63 |
markdown=True,
|
| 64 |
)
|
| 65 |
|
| 66 |
return (
|
| 67 |
+
"✅ PDF loaded successfully! You can now chat with the document.",
|
| 68 |
+
file_path,
|
| 69 |
)
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
return f"❌ Error: {str(e)}", None
|
| 73 |
|
| 74 |
|
| 75 |
+
def chat_fn(message, history):
|
|
|
|
| 76 |
global agent
|
| 77 |
|
| 78 |
if agent is None:
|
|
|
|
| 85 |
return history + [(message, f"Error: {str(e)}")]
|
| 86 |
|
| 87 |
|
| 88 |
+
def generate_code():
|
| 89 |
+
global current_file_path
|
|
|
|
| 90 |
|
| 91 |
+
if current_file_path is None:
|
| 92 |
return "Please upload a PDF first!"
|
| 93 |
|
| 94 |
+
return f'''# Neosantara RAG Agent
|
| 95 |
# Generated from RAG Demo Tool
|
| 96 |
|
| 97 |
from agno.agent import Agent
|
| 98 |
+
from agno.knowledge.knowledge import Knowledge
|
| 99 |
+
from agno.knowledge.reader.pdf_reader import PDFReader
|
| 100 |
+
from agno.vectordb.lancedb import LanceDb
|
| 101 |
+
from agno.knowledge.embedder.openai import OpenAIEmbedder
|
| 102 |
from agno.models.neosantara import Neosantara
|
| 103 |
|
| 104 |
+
NEOSANTARAA_API_KEY = "your-api-key-here"
|
|
|
|
| 105 |
MODEL_ID = "{MODEL_ID}"
|
| 106 |
EMBEDDER_ID = "{EMBEDDER_ID}"
|
| 107 |
|
|
|
|
| 108 |
embedder = OpenAIEmbedder(
|
| 109 |
id=EMBEDDER_ID,
|
| 110 |
+
api_key=NEOSANTARAA_API_KEY,
|
| 111 |
base_url="https://api.neosantara.xyz/v1"
|
| 112 |
)
|
| 113 |
|
|
|
|
| 114 |
vector_db = LanceDb(
|
| 115 |
table_name="my_doc",
|
| 116 |
uri="/tmp/lancedb",
|
| 117 |
embedder=embedder
|
| 118 |
)
|
| 119 |
|
| 120 |
+
knowledge = Knowledge(
|
| 121 |
+
vector_db=vector_db,
|
| 122 |
+
reader=PDFReader(chunk=True)
|
|
|
|
| 123 |
)
|
| 124 |
+
knowledge.insert(path="{current_file_path}")
|
| 125 |
|
|
|
|
| 126 |
agent = Agent(
|
| 127 |
model=Neosantara(
|
| 128 |
id=MODEL_ID,
|
| 129 |
+
api_key=NEOSANTARAA_API_KEY,
|
| 130 |
base_url="https://api.neosantara.xyz/v1"
|
| 131 |
),
|
| 132 |
+
knowledge=knowledge,
|
| 133 |
search_knowledge=True,
|
| 134 |
markdown=True
|
| 135 |
)
|
| 136 |
|
|
|
|
| 137 |
agent.print_response("Apa isi dokumen ini?")
|
| 138 |
'''
|
|
|
|
| 139 |
|
| 140 |
|
| 141 |
+
def clear_fn():
|
| 142 |
+
global knowledge_base, agent, current_file_path
|
|
|
|
| 143 |
knowledge_base = None
|
| 144 |
agent = None
|
| 145 |
+
current_file_path = None
|
| 146 |
+
return None, [], "Session cleared. Upload a new PDF."
|
| 147 |
|
| 148 |
|
|
|
|
| 149 |
with gr.Blocks(title="Neosantara RAG Demo") as demo:
|
| 150 |
gr.Markdown("# 📄 Neosantara RAG Demo")
|
| 151 |
+
gr.Markdown("Upload a PDF, chat with AI, and export the code!")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column(scale=1):
|
| 155 |
+
file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 156 |
+
process_btn = gr.Button("Process PDF", variant="primary")
|
| 157 |
+
status = gr.Textbox(label="Status", interactive=False)
|
|
|
|
|
|
|
| 158 |
clear_btn = gr.Button("Clear Session")
|
| 159 |
|
| 160 |
with gr.Column(scale=2):
|
| 161 |
chatbot = gr.Chatbot(label="Chat", height=400)
|
| 162 |
+
msg = gr.Textbox(label="Message", placeholder="Ask about your document...")
|
|
|
|
|
|
|
| 163 |
send_btn = gr.Button("Send", variant="primary")
|
| 164 |
+
export_btn = gr.Button("📋 Export Code")
|
| 165 |
+
code_output = gr.Code(label="Generated Code", language="python")
|
| 166 |
|
| 167 |
+
process_btn.click(fn=process_pdf, inputs=[file_input], outputs=[status, chatbot])
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
send_btn.click(fn=chat_fn, inputs=[msg, chatbot], outputs=[chatbot])
|
| 170 |
+
msg.submit(fn=chat_fn, inputs=[msg, chatbot], outputs=[chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
export_btn.click(fn=generate_code, inputs=[], outputs=[code_output])
|
| 173 |
|
| 174 |
+
clear_btn.click(fn=clear_fn, inputs=[], outputs=[file_input, chatbot, status])
|
|
|
|
|
|
|
| 175 |
|
|
|
|
| 176 |
if __name__ == "__main__":
|
| 177 |
+
demo.launch()
|