Spaces:
Sleeping
Sleeping
Commit
·
422431d
1
Parent(s):
a9e55b8
HF Spaces
Browse files- .gitignore +30 -0
- Dockerfile +14 -0
- README.md +17 -0
- api.py +5 -3
- app.py +49 -0
- app_gradio.py +49 -0
.gitignore
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
*.egg-info/
|
| 19 |
+
.installed.cfg
|
| 20 |
+
*.egg
|
| 21 |
+
|
| 22 |
+
# Environment variables
|
| 23 |
+
.env
|
| 24 |
+
|
| 25 |
+
# Misc
|
| 26 |
+
.DS_Store
|
| 27 |
+
.vscode/
|
| 28 |
+
.idea/
|
| 29 |
+
*.swp
|
| 30 |
+
*.swo
|
Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Expose the port that FastAPI will run on
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
# Command to run the FastAPI application
|
| 14 |
+
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -23,3 +23,20 @@ uvicorn api:app --reload --host 0.0.0.0 --port 8000
|
|
| 23 |
---
|
| 24 |
|
| 25 |
**Do not commit your real `.env` file! Use `.env.example` for reference.**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
---
|
| 24 |
|
| 25 |
**Do not commit your real `.env` file! Use `.env.example` for reference.**
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## Hugging Face Spaces Configuration
|
| 30 |
+
|
| 31 |
+
The following configuration is for deploying to Hugging Face Spaces:
|
| 32 |
+
|
| 33 |
+
```yaml
|
| 34 |
+
title: ChatDocxAI
|
| 35 |
+
emoji: 📄
|
| 36 |
+
colorFrom: indigo
|
| 37 |
+
colorTo: blue
|
| 38 |
+
sdk: gradio
|
| 39 |
+
pinned: false
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
For a FastAPI backend with Gradio interface on Hugging Face Spaces.
|
api.py
CHANGED
|
@@ -27,7 +27,9 @@ app = FastAPI()
|
|
| 27 |
# Define the specific origins that are allowed to make requests to your API
|
| 28 |
origins = [
|
| 29 |
"http://localhost:3000", # For local development
|
| 30 |
-
"https://chat-docx-ai-vercel.vercel.app",
|
|
|
|
|
|
|
| 31 |
]
|
| 32 |
|
| 33 |
app.add_middleware(
|
|
@@ -47,7 +49,7 @@ async def options_upload():
|
|
| 47 |
return JSONResponse(
|
| 48 |
content={"status": "ok"},
|
| 49 |
headers={
|
| 50 |
-
"Access-Control-Allow-Origin": "
|
| 51 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 52 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 53 |
},
|
|
@@ -57,7 +59,7 @@ async def options_upload():
|
|
| 57 |
@app.post("/upload")
|
| 58 |
async def upload(files: List[UploadFile] = File(...)):
|
| 59 |
headers = {
|
| 60 |
-
"Access-Control-Allow-Origin": "
|
| 61 |
}
|
| 62 |
try:
|
| 63 |
if not files:
|
|
|
|
| 27 |
# Define the specific origins that are allowed to make requests to your API
|
| 28 |
origins = [
|
| 29 |
"http://localhost:3000", # For local development
|
| 30 |
+
"https://chat-docx-ai-vercel.vercel.app",
|
| 31 |
+
"https://huggingface.co", # Hugging Face Spaces domain
|
| 32 |
+
"https://codegeass321-chatdocxai.hf.space", # Your specific HF space
|
| 33 |
]
|
| 34 |
|
| 35 |
app.add_middleware(
|
|
|
|
| 49 |
return JSONResponse(
|
| 50 |
content={"status": "ok"},
|
| 51 |
headers={
|
| 52 |
+
"Access-Control-Allow-Origin": "*",
|
| 53 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 54 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 55 |
},
|
|
|
|
| 59 |
@app.post("/upload")
|
| 60 |
async def upload(files: List[UploadFile] = File(...)):
|
| 61 |
headers = {
|
| 62 |
+
"Access-Control-Allow-Origin": "*"
|
| 63 |
}
|
| 64 |
try:
|
| 65 |
if not files:
|
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from api import app
|
| 3 |
+
import uvicorn
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Create a simple Gradio interface
|
| 8 |
+
def create_interface():
|
| 9 |
+
with gr.Blocks(title="ChatDocxAI Backend") as interface:
|
| 10 |
+
gr.Markdown("# ChatDocxAI Backend")
|
| 11 |
+
gr.Markdown("""
|
| 12 |
+
This is the backend server for ChatDocxAI. It provides the following endpoints:
|
| 13 |
+
|
| 14 |
+
- `/upload` - Upload documents
|
| 15 |
+
- `/ask` - Ask questions about uploaded documents
|
| 16 |
+
|
| 17 |
+
The frontend should be configured to communicate with this backend.
|
| 18 |
+
""")
|
| 19 |
+
|
| 20 |
+
with gr.Row():
|
| 21 |
+
with gr.Column():
|
| 22 |
+
gr.Markdown("## Server Status")
|
| 23 |
+
status = gr.Textbox(value="Server is running", label="Status")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
gr.Markdown("## API Documentation")
|
| 28 |
+
doc_link = gr.HTML(f"<a href='/docs' target='_blank'>View FastAPI Docs</a>")
|
| 29 |
+
|
| 30 |
+
return interface
|
| 31 |
+
|
| 32 |
+
# Function to start FastAPI in a separate thread
|
| 33 |
+
def start_fastapi():
|
| 34 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 35 |
+
|
| 36 |
+
# Start FastAPI in a separate thread
|
| 37 |
+
fastapi_thread = threading.Thread(target=start_fastapi)
|
| 38 |
+
fastapi_thread.daemon = True
|
| 39 |
+
fastapi_thread.start()
|
| 40 |
+
|
| 41 |
+
# Allow some time for FastAPI to start
|
| 42 |
+
time.sleep(2)
|
| 43 |
+
|
| 44 |
+
# Create and launch the Gradio interface
|
| 45 |
+
interface = create_interface()
|
| 46 |
+
|
| 47 |
+
# Launch the interface with Gradio defaults (no specific port)
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
interface.launch()
|
app_gradio.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from api import app
|
| 3 |
+
import uvicorn
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# Create a simple Gradio interface
|
| 8 |
+
def create_interface():
|
| 9 |
+
with gr.Blocks(title="ChatDocxAI Backend") as interface:
|
| 10 |
+
gr.Markdown("# ChatDocxAI Backend")
|
| 11 |
+
gr.Markdown("""
|
| 12 |
+
This is the backend server for ChatDocxAI. It provides the following endpoints:
|
| 13 |
+
|
| 14 |
+
- `/upload` - Upload documents
|
| 15 |
+
- `/ask` - Ask questions about uploaded documents
|
| 16 |
+
|
| 17 |
+
The frontend should be configured to communicate with this backend.
|
| 18 |
+
""")
|
| 19 |
+
|
| 20 |
+
with gr.Row():
|
| 21 |
+
with gr.Column():
|
| 22 |
+
gr.Markdown("## Server Status")
|
| 23 |
+
status = gr.Textbox(value="Server is running", label="Status")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
gr.Markdown("## API Documentation")
|
| 28 |
+
doc_link = gr.HTML(f"<a href='/docs' target='_blank'>View FastAPI Docs</a>")
|
| 29 |
+
|
| 30 |
+
return interface
|
| 31 |
+
|
| 32 |
+
# Function to start FastAPI in a separate thread
|
| 33 |
+
def start_fastapi():
|
| 34 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 35 |
+
|
| 36 |
+
# Start FastAPI in a separate thread
|
| 37 |
+
fastapi_thread = threading.Thread(target=start_fastapi)
|
| 38 |
+
fastapi_thread.daemon = True
|
| 39 |
+
fastapi_thread.start()
|
| 40 |
+
|
| 41 |
+
# Allow some time for FastAPI to start
|
| 42 |
+
time.sleep(2)
|
| 43 |
+
|
| 44 |
+
# Create and launch the Gradio interface
|
| 45 |
+
interface = create_interface()
|
| 46 |
+
|
| 47 |
+
# Launch the interface
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
interface.launch(server_port=7861) # Use a different port than FastAPI
|