Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,41 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from scripts.
|
| 4 |
-
|
| 5 |
-
OPENAI_KEY = os.getenv("OPENAI_API_KEY", None)
|
| 6 |
-
MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
|
| 7 |
-
|
| 8 |
-
if not OPENAI_KEY:
|
| 9 |
-
print("WARNING: OPENAI_API_KEY not set. The app may fail at runtime.")
|
| 10 |
-
|
| 11 |
-
# Build the router once (keeps vectorstore & models in memory)
|
| 12 |
-
router = build_router_chain(model_name=MODEL_NAME)
|
| 13 |
-
|
| 14 |
-
def chat_fn(message, history):
|
| 15 |
-
if not message:
|
| 16 |
-
return history, ""
|
| 17 |
-
# call router
|
| 18 |
-
result = router.invoke({"input": message})
|
| 19 |
-
# RetrievalQA returns dict with 'result' key (and maybe 'source_documents')
|
| 20 |
-
answer = result.get("result") if isinstance(result, dict) else str(result)
|
| 21 |
-
# append sources if present
|
| 22 |
-
sources = None
|
| 23 |
-
if isinstance(result, dict) and "source_documents" in result and result["source_documents"]:
|
| 24 |
-
try:
|
| 25 |
-
sources = list({str(d.metadata.get("source", "unknown")) for d in result["source_documents"]})
|
| 26 |
-
except Exception:
|
| 27 |
-
sources = None
|
| 28 |
-
if sources:
|
| 29 |
-
answer = f"{answer}\n\nπ Sources: {', '.join(sources)}"
|
| 30 |
-
history.append((message, answer))
|
| 31 |
-
return history, ""
|
| 32 |
-
|
| 33 |
-
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("## π Course Assistant β Chat with your course files")
|
| 35 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 36 |
-
txt = gr.Textbox(show_label=False, placeholder="Ask about the course...")
|
| 37 |
-
txt.submit(chat_fn, [txt, chatbot], [chatbot, txt])
|
| 38 |
-
txt.submit(lambda: None, None, txt) # clear input
|
| 39 |
-
|
| 40 |
-
if __name__ == "__main__":
|
| 41 |
-
demo.launch(server_port=int(os.getenv("PORT", 7860)))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from scripts.router_chain import build_router_chain
|
| 4 |
+
|
| 5 |
+
OPENAI_KEY = os.getenv("OPENAI_API_KEY", None)
|
| 6 |
+
MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
|
| 7 |
+
|
| 8 |
+
if not OPENAI_KEY:
|
| 9 |
+
print("WARNING: OPENAI_API_KEY not set. The app may fail at runtime.")
|
| 10 |
+
|
| 11 |
+
# Build the router once (keeps vectorstore & models in memory)
|
| 12 |
+
router = build_router_chain(model_name=MODEL_NAME)
|
| 13 |
+
|
| 14 |
+
def chat_fn(message, history):
|
| 15 |
+
if not message:
|
| 16 |
+
return history, ""
|
| 17 |
+
# call router
|
| 18 |
+
result = router.invoke({"input": message})
|
| 19 |
+
# RetrievalQA returns dict with 'result' key (and maybe 'source_documents')
|
| 20 |
+
answer = result.get("result") if isinstance(result, dict) else str(result)
|
| 21 |
+
# append sources if present
|
| 22 |
+
sources = None
|
| 23 |
+
if isinstance(result, dict) and "source_documents" in result and result["source_documents"]:
|
| 24 |
+
try:
|
| 25 |
+
sources = list({str(d.metadata.get("source", "unknown")) for d in result["source_documents"]})
|
| 26 |
+
except Exception:
|
| 27 |
+
sources = None
|
| 28 |
+
if sources:
|
| 29 |
+
answer = f"{answer}\n\nπ Sources: {', '.join(sources)}"
|
| 30 |
+
history.append((message, answer))
|
| 31 |
+
return history, ""
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("## π Course Assistant β Chat with your course files")
|
| 35 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 36 |
+
txt = gr.Textbox(show_label=False, placeholder="Ask about the course...")
|
| 37 |
+
txt.submit(chat_fn, [txt, chatbot], [chatbot, txt])
|
| 38 |
+
txt.submit(lambda: None, None, txt) # clear input
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch(server_port=int(os.getenv("PORT", 7860)))
|