File size: 2,732 Bytes
56fd21a b88df70 ea4670b b88df70 68b95db b88df70 68b95db 56fd21a b88df70 68b95db b88df70 080d622 b88df70 c629e49 b88df70 2aa225e b88df70 56fd21a b88df70 5193d26 b88df70 0e8391a b88df70 5193d26 b88df70 68b95db b88df70 68b95db b88df70 ea4670b b88df70 68b95db b88df70 68b95db b88df70 68b95db b88df70 68b95db b88df70 68b95db b88df70 84f1cd4 56fd21a b88df70 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import os
import threading
import time
import gradio as gr
import uvicorn
import requests
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
# Import our main application
from fastapi_server import app as fastapi_app
# Run FastAPI server in a separate thread
def run_fastapi():
uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
# Start FastAPI in a background thread
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
fastapi_thread.start()
# Wait for FastAPI to start
time.sleep(5)
# Create a Gradio interface that will proxy requests to FastAPI
def chat_with_api(message, conversation_id=None):
try:
response = requests.post(
"http://127.0.0.1:8000/chat",
json={"message": message, "conversation_id": conversation_id}
)
if response.status_code == 200:
data = response.json()
return data["response"], data["conversation_id"]
else:
return f"Error: {response.status_code} - {response.text}", conversation_id
except Exception as e:
return f"API connection error: {str(e)}", conversation_id
def build_kb():
try:
response = requests.post("http://127.0.0.1:8000/build-kb")
if response.status_code == 200:
return f"Success: {response.json()['message']}"
else:
return f"Error: {response.status_code} - {response.text}"
except Exception as e:
return f"API connection error: {str(e)}"
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Status Law Assistant")
with gr.Row():
with gr.Column():
build_kb_btn = gr.Button("Create/Update Knowledge Base")
kb_status = gr.Textbox(label="Knowledge Base Status")
build_kb_btn.click(build_kb, inputs=None, outputs=kb_status)
conversation_id = gr.State(None)
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(label="Chat with Assistant")
msg = gr.Textbox(label="Your Question")
def respond(message, chat_history, conv_id):
if not message.strip():
return chat_history, conv_id
chat_history.append([message, ""])
response, new_conv_id = chat_with_api(message, conv_id)
chat_history[-1][1] = response
return chat_history, new_conv_id
msg.submit(respond, [msg, chatbot, conversation_id], [chatbot, conversation_id])
if __name__ == "__main__":
# Launch Gradio interface
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|