Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .dockerignore +14 -0
- .gitignore +17 -0
- Dockerfile +19 -0
- README.md +45 -10
- app.py +66 -0
- requirements.txt +4 -0
.dockerignore
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
ENV/
|
| 9 |
+
.venv
|
| 10 |
+
.git
|
| 11 |
+
.gitignore
|
| 12 |
+
README.md
|
| 13 |
+
.DS_Store
|
| 14 |
+
|
.gitignore
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
*.so
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
ENV/
|
| 9 |
+
.venv
|
| 10 |
+
*.egg-info/
|
| 11 |
+
dist/
|
| 12 |
+
build/
|
| 13 |
+
.pytest_cache/
|
| 14 |
+
.coverage
|
| 15 |
+
htmlcov/
|
| 16 |
+
.DS_Store
|
| 17 |
+
|
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy requirements file
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
|
| 8 |
+
# Install dependencies
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy application code
|
| 12 |
+
COPY app.py .
|
| 13 |
+
|
| 14 |
+
# Expose port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run the application
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
| 19 |
+
|
README.md
CHANGED
|
@@ -1,10 +1,45 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
---
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Wikipedia Lookup API
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Wikipedia Lookup API
|
| 11 |
+
|
| 12 |
+
A simple FastAPI application that looks up Wikipedia URLs for any topic.
|
| 13 |
+
|
| 14 |
+
## Usage
|
| 15 |
+
|
| 16 |
+
Send a GET request to the root endpoint (`/`) with a `topic` query parameter:
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
GET /?topic=Larry Page
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
### Response
|
| 23 |
+
|
| 24 |
+
```json
|
| 25 |
+
{
|
| 26 |
+
"topic": "Larry Page",
|
| 27 |
+
"url": "https://en.wikipedia.org/wiki/Larry_Page"
|
| 28 |
+
}
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Endpoints
|
| 32 |
+
|
| 33 |
+
- `GET /` - Look up a Wikipedia URL for a topic
|
| 34 |
+
- `GET /health` - Health check endpoint
|
| 35 |
+
|
| 36 |
+
## Example
|
| 37 |
+
|
| 38 |
+
Visit: `https://your-space-url.hf.space/?topic=Python`
|
| 39 |
+
|
| 40 |
+
## Technology Stack
|
| 41 |
+
|
| 42 |
+
- FastAPI
|
| 43 |
+
- Wikipedia Python library
|
| 44 |
+
- Uvicorn ASGI server
|
| 45 |
+
|
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import wikipedia
|
| 4 |
+
|
| 5 |
+
app = FastAPI(
|
| 6 |
+
title="Wikipedia Lookup API",
|
| 7 |
+
description="A simple API to look up Wikipedia URLs for any topic",
|
| 8 |
+
version="1.0.0"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@app.get("/")
|
| 13 |
+
async def lookup_wikipedia(topic: str = Query(..., description="The topic to search for on Wikipedia")):
|
| 14 |
+
"""
|
| 15 |
+
Look up a Wikipedia URL for a given topic.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
topic: The topic to search for
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
JSON with the topic and its Wikipedia URL
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
# Search for the topic and get the first result
|
| 25 |
+
page = wikipedia.page(topic)
|
| 26 |
+
wikipedia_url = page.url
|
| 27 |
+
|
| 28 |
+
return JSONResponse(
|
| 29 |
+
content={
|
| 30 |
+
"topic": topic,
|
| 31 |
+
"url": wikipedia_url
|
| 32 |
+
}
|
| 33 |
+
)
|
| 34 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 35 |
+
# If there are multiple matches, use the first one
|
| 36 |
+
page = wikipedia.page(e.options[0])
|
| 37 |
+
wikipedia_url = page.url
|
| 38 |
+
|
| 39 |
+
return JSONResponse(
|
| 40 |
+
content={
|
| 41 |
+
"topic": topic,
|
| 42 |
+
"url": wikipedia_url
|
| 43 |
+
}
|
| 44 |
+
)
|
| 45 |
+
except wikipedia.exceptions.PageError:
|
| 46 |
+
# If no page is found, return an error
|
| 47 |
+
return JSONResponse(
|
| 48 |
+
status_code=404,
|
| 49 |
+
content={
|
| 50 |
+
"error": f"No Wikipedia page found for '{topic}'"
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return JSONResponse(
|
| 55 |
+
status_code=500,
|
| 56 |
+
content={
|
| 57 |
+
"error": f"An error occurred: {str(e)}"
|
| 58 |
+
}
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@app.get("/health")
|
| 63 |
+
async def health_check():
|
| 64 |
+
"""Health check endpoint"""
|
| 65 |
+
return {"status": "healthy"}
|
| 66 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.104.1
|
| 2 |
+
uvicorn==0.24.0
|
| 3 |
+
wikipedia==1.4.0
|
| 4 |
+
|