witchertech commited on
Commit
553696d
·
1 Parent(s): 7e6b944

Initial commit

Browse files
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. main.py +40 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Copy requirements first for better caching
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 main.py .
13
+
14
+ # Expose port 7860 (Hugging Face Spaces default)
15
+ EXPOSE 7860
16
+
17
+ # Run the application
18
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import wikipedia
4
+
5
+ app = FastAPI()
6
+
7
+ # Add CORS middleware
8
+ app.add_middleware(
9
+ CORSMiddleware,
10
+ allow_origins=["*"],
11
+ allow_credentials=True,
12
+ allow_methods=["*"],
13
+ allow_headers=["*"],
14
+ )
15
+
16
+ @app.get("/")
17
+ async def get_wikipedia_url(topic: str):
18
+ try:
19
+ # Search for the page
20
+ page = wikipedia.page(topic, auto_suggest=True)
21
+ # Get the URL from the page object
22
+ url = page.url
23
+ return {"topic": topic, "url": url}
24
+ except wikipedia.exceptions.DisambiguationError as e:
25
+ # If multiple pages exist, take the first option
26
+ try:
27
+ page = wikipedia.page(e.options[0])
28
+ return {"topic": topic, "url": page.url}
29
+ except:
30
+ return {"topic": topic, "url": "https://en.wikipedia.org/wiki/Main_Page"}
31
+ except wikipedia.exceptions.PageError:
32
+ # Page doesn't exist
33
+ return {"topic": topic, "url": "https://en.wikipedia.org/wiki/Main_Page"}
34
+ except Exception as e:
35
+ # Any other error
36
+ return {"topic": topic, "url": "https://en.wikipedia.org/wiki/Main_Page"}
37
+
38
+ @app.get("/health")
39
+ async def health_check():
40
+ return {"status": "healthy"}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi==0.104.1
2
+ uvicorn==0.24.0
3
+ wikipedia==1.4.0