File size: 2,051 Bytes
422431d
7036bcd
422431d
 
 
0068013
7036bcd
0068013
 
 
 
7036bcd
 
 
 
 
 
422431d
7036bcd
422431d
 
 
0068013
422431d
 
7036bcd
 
 
422431d
 
 
 
 
 
 
 
 
 
 
 
7036bcd
422431d
 
 
7036bcd
 
422431d
7036bcd
 
 
422431d
7036bcd
 
422431d
 
7036bcd
 
 
 
 
422431d
7036bcd
 
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
import gradio as gr
from api import app as fastapi_app
import uvicorn
import threading
import time
import os
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Create a Gradio app that will also host the FastAPI app
def create_interface():
    with gr.Blocks(title="ChatDocxAI Backend") as interface:
        gr.Markdown("# ChatDocxAI Backend")
        gr.Markdown(f"""
        This is the backend server for ChatDocxAI. It provides the following endpoints:
        
        - `/api/upload` - Upload documents
        - `/api/ask` - Ask questions about uploaded documents
        - `/api/status` - Check API status
        
        The frontend should be configured to communicate with this backend.
        """)

        with gr.Row():
            with gr.Column():
                gr.Markdown("## Server Status")
                status = gr.Textbox(value="Server is running", label="Status")
                
        with gr.Row():
            with gr.Column():
                gr.Markdown("## API Documentation")
                doc_link = gr.HTML(f"<a href='/api/docs' target='_blank'>View FastAPI Docs</a>")
    
    return interface

# Create a new FastAPI app that will mount both the original FastAPI app and the Gradio app
app = FastAPI()

# Mount the original FastAPI app under the /api prefix
logger.info("Mounting FastAPI app at /api")
app.mount("/api", fastapi_app)

# Create the Gradio interface
logger.info("Creating Gradio interface")
interface = create_interface()

# Gradio blocks to FastAPI app
logger.info("Mounting Gradio app at /")
app = gr.mount_gradio_app(app, interface, path="/")

# When running directly, start the app
if __name__ == "__main__":
    logger.info("Starting server on port 7860")
    uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")