Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +27 -0
- app.py +33 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set up a new user named "user" with user ID 1000
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
|
| 6 |
+
# Switch to the "user" user
|
| 7 |
+
USER user
|
| 8 |
+
|
| 9 |
+
# Set home to the user's home directory
|
| 10 |
+
ENV HOME=/home/user \
|
| 11 |
+
PATH=/home/user/.local/bin:$PATH
|
| 12 |
+
|
| 13 |
+
# Set the working directory to the user's home directory
|
| 14 |
+
WORKDIR $HOME/app
|
| 15 |
+
|
| 16 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 17 |
+
COPY --chown=user . $HOME/app
|
| 18 |
+
|
| 19 |
+
# Install requirements
|
| 20 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 21 |
+
pip install --no-cache-dir -r requirements.txt
|
| 22 |
+
|
| 23 |
+
# Expose the port that the application listens on.
|
| 24 |
+
EXPOSE 7860
|
| 25 |
+
|
| 26 |
+
# Run the application.
|
| 27 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
from duckduckgo_search import DDGS
|
| 5 |
+
import logging
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
templates = Jinja2Templates(directory="templates")
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
@app.get("/", response_class=HTMLResponse)
|
| 14 |
+
async def read_root(request: Request):
|
| 15 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 16 |
+
|
| 17 |
+
@app.get("/api/search")
|
| 18 |
+
async def search(q: str):
|
| 19 |
+
if not q: return {"results": []}
|
| 20 |
+
try:
|
| 21 |
+
# DDGS context manager is recommended
|
| 22 |
+
with DDGS() as ddgs:
|
| 23 |
+
# text() returns an iterator/generator in newer versions, convert to list
|
| 24 |
+
# max_results is valid in newer versions of duckduckgo_search
|
| 25 |
+
results = list(ddgs.text(q, max_results=10))
|
| 26 |
+
return {"results": results}
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logger.error(f"Search error: {e}")
|
| 29 |
+
# In production we might want to hide the exact error, but for dev this is fine
|
| 30 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.109.0
|
| 2 |
+
uvicorn[standard]>=0.27.0
|
| 3 |
+
duckduckgo-search>=5.0.0
|
| 4 |
+
jinja2>=3.1.3
|