mugiwarafx
commited on
Commit
•
acde423
1
Parent(s):
8bf7724
add logging
Browse files- Dockerfile +9 -3
- app.py +16 -0
Dockerfile
CHANGED
@@ -1,13 +1,19 @@
|
|
|
|
1 |
FROM python:3.9
|
2 |
|
|
|
3 |
RUN useradd -m -u 1000 user
|
4 |
-
|
5 |
WORKDIR /app
|
6 |
|
|
|
7 |
COPY --chown=user ./requirements.txt requirements.txt
|
8 |
-
|
9 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
10 |
|
|
|
11 |
COPY --chown=user . /app
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
FROM python:3.9
|
3 |
|
4 |
+
# Create a user and set the working directory
|
5 |
RUN useradd -m -u 1000 user
|
|
|
6 |
WORKDIR /app
|
7 |
|
8 |
+
# Copy the requirements file and install dependencies
|
9 |
COPY --chown=user ./requirements.txt requirements.txt
|
|
|
10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
|
12 |
+
# Copy the rest of the application code
|
13 |
COPY --chown=user . /app
|
14 |
|
15 |
+
# Expose the required port
|
16 |
+
EXPOSE 7860
|
17 |
+
|
18 |
+
# Run the FastAPI application with logging
|
19 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug"]
|
app.py
CHANGED
@@ -1,8 +1,13 @@
|
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from bs4 import BeautifulSoup
|
4 |
from typing import List, Dict
|
5 |
|
|
|
|
|
|
|
|
|
6 |
app = FastAPI()
|
7 |
|
8 |
all_html_tags = {
|
@@ -27,6 +32,7 @@ class HTMLOutput(BaseModel):
|
|
27 |
|
28 |
|
29 |
def extract_html_tags(html_code: str) -> Dict[str, List[str]]:
|
|
|
30 |
soup = BeautifulSoup(html_code, "html.parser")
|
31 |
tags_used = {tag.name for tag in soup.find_all()}
|
32 |
tags_not_used = all_html_tags - tags_used
|
@@ -40,6 +46,16 @@ def extract_html_tags(html_code: str) -> Dict[str, List[str]]:
|
|
40 |
async def extract_tags(input: HTMLInput):
|
41 |
try:
|
42 |
result = extract_html_tags(input.html_code)
|
|
|
43 |
return HTMLOutput(**result)
|
44 |
except Exception as e:
|
|
|
45 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from pydantic import BaseModel
|
4 |
from bs4 import BeautifulSoup
|
5 |
from typing import List, Dict
|
6 |
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.DEBUG)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
app = FastAPI()
|
12 |
|
13 |
all_html_tags = {
|
|
|
32 |
|
33 |
|
34 |
def extract_html_tags(html_code: str) -> Dict[str, List[str]]:
|
35 |
+
logger.debug(f"Extracting HTML tags from code: {html_code}")
|
36 |
soup = BeautifulSoup(html_code, "html.parser")
|
37 |
tags_used = {tag.name for tag in soup.find_all()}
|
38 |
tags_not_used = all_html_tags - tags_used
|
|
|
46 |
async def extract_tags(input: HTMLInput):
|
47 |
try:
|
48 |
result = extract_html_tags(input.html_code)
|
49 |
+
logger.debug(f"Extraction result: {result}")
|
50 |
return HTMLOutput(**result)
|
51 |
except Exception as e:
|
52 |
+
logger.error(f"Error during extraction: {e}")
|
53 |
raise HTTPException(status_code=500, detail=str(e))
|
54 |
+
|
55 |
+
# Add a simple root endpoint to confirm the server is running
|
56 |
+
|
57 |
+
|
58 |
+
@app.get("/")
|
59 |
+
async def root():
|
60 |
+
logger.debug("Root endpoint called")
|
61 |
+
return {"message": "FastAPI application is running"}
|