# 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 = "

📚 Retrieved Context

\n\n" # for doc in docs: # title = doc.metadata.get("title", "Unknown") # doc_type = doc.metadata.get("doc_type", "") # result += f"
" # result += f"

📄 {title}" # if doc_type: # result += f"  |  {doc_type}" # result += "

" # result += f"
{doc.page_content}
\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="

Retrieved context will appear here...

", # ) # 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}] @app.get("/health") def health(): return {"status": "ok"} @app.post("/query", response_model=QueryResponse) 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}