Spaces:
Runtime error
Runtime error
| # import gradio as gr | |
| # from dotenv import load_dotenv | |
| # from answer import answer_question | |
| # load_dotenv(override=True) | |
| # # ----------------------------- | |
| # # FORMAT CONTEXT PANEL | |
| # # ----------------------------- | |
| # def format_context(docs): | |
| # result = "<h2 style='color: #ff7800;'>π Retrieved Context</h2>\n\n" | |
| # for doc in docs: | |
| # title = doc.metadata.get("title", "Unknown") | |
| # doc_type = doc.metadata.get("doc_type", "") | |
| # result += f"<hr/>" | |
| # result += f"<p><strong style='color: #ff7800;'>π {title}</strong>" | |
| # if doc_type: | |
| # result += f" | <code>{doc_type}</code>" | |
| # result += "</p>" | |
| # result += f"<pre style='background:#f4f4f4; padding:10px; border-radius:6px; font-size:12px; white-space:pre-wrap;'>{doc.page_content}</pre>\n\n" | |
| # return result | |
| # # ----------------------------- | |
| # # CHAT LOGIC | |
| # # ----------------------------- | |
| # def chat(history): | |
| # last_message = history[-1]["content"] | |
| # prior = history[:-1] | |
| # answer, docs = answer_question(last_message, prior) | |
| # history.append({"role": "assistant", "content": answer}) | |
| # return history, format_context(docs) | |
| # # ----------------------------- | |
| # # UI | |
| # # ----------------------------- | |
| # def main(): | |
| # def put_message_in_chatbot(message, history): | |
| # return "", history + [{"role": "user", "content": message}] | |
| # theme = gr.themes.Soft(font=["Inter", "system-ui", "sans-serif"]) | |
| # with gr.Blocks(title="Socrox ChatBot") as ui: | |
| # gr.Markdown("# π οΈ Socrox ChatBot\nAsk me anything about the platform.") | |
| # with gr.Row(): | |
| # with gr.Column(scale=1): | |
| # chatbot = gr.Chatbot( | |
| # label="π¬ Conversation", | |
| # height=600, | |
| # render_markdown=True, # π renders newlines and markdown | |
| # ) | |
| # message = gr.Textbox( | |
| # placeholder="Ask anything about Socrox...", | |
| # show_label=False, | |
| # submit_btn=True, # shows send button on textbox | |
| # ) | |
| # with gr.Column(scale=1): | |
| # context_display = gr.HTML( | |
| # label="π Retrieved Context", | |
| # value="<p style='color:gray;'>Retrieved context will appear here...</p>", | |
| # ) | |
| # message.submit( | |
| # put_message_in_chatbot, | |
| # inputs=[message, chatbot], | |
| # outputs=[message, chatbot] | |
| # ).then( | |
| # chat, | |
| # inputs=chatbot, | |
| # outputs=[chatbot, context_display] | |
| # ) | |
| # ui.launch( | |
| # server_name="0.0.0.0", | |
| # server_port=7860, | |
| # inbrowser=True, | |
| # share=False, | |
| # theme=theme | |
| # ) | |
| # if __name__ == "__main__": | |
| # main() | |
| # app.py | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from answer import answer_question | |
| from dotenv import load_dotenv | |
| load_dotenv(override=True) | |
| app = FastAPI(title="Socrox RAG API") | |
| class QueryRequest(BaseModel): | |
| question: str | |
| history: list[dict] = [] # [{"role": "user", "content": "..."}] | |
| class QueryResponse(BaseModel): | |
| answer: str | |
| context: list[dict] # [{title, doc_type, content}] | |
| def health(): | |
| return {"status": "ok"} | |
| def query(req: QueryRequest): | |
| answer, docs = answer_question(req.question, req.history) | |
| context = [ | |
| { | |
| "title": doc.metadata.get("title", "Unknown"), | |
| "doc_type": doc.metadata.get("doc_type", ""), | |
| "content": doc.page_content, | |
| } | |
| for doc in docs | |
| ] | |
| return {"answer": answer, "context": context} |