sccastillo commited on
Commit
113ef3e
·
1 Parent(s): 476e500

Fix Hugging Face Spaces deployment: enhance app.py, optimize Dockerfile, add main.py alternative

Browse files
.gitignore CHANGED
@@ -1 +1,62 @@
1
- .researcher
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Environment variables (contains sensitive API keys)
2
+ .env
3
+ .env.local
4
+ .env.production
5
+ .env.staging
6
+ *.env
7
+
8
+ # Python cache
9
+ __pycache__/
10
+ *.py[cod]
11
+ *$py.class
12
+ *.so
13
+
14
+ # Distribution / packaging
15
+ .Python
16
+ build/
17
+ develop-eggs/
18
+ dist/
19
+ downloads/
20
+ eggs/
21
+ .eggs/
22
+ lib/
23
+ lib64/
24
+ parts/
25
+ sdist/
26
+ var/
27
+ wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+
32
+ # Virtual environments
33
+ venv/
34
+ env/
35
+ ENV/
36
+ env.bak/
37
+ venv.bak/
38
+
39
+ # IDE files
40
+ .vscode/
41
+ .idea/
42
+ *.swp
43
+ *.swo
44
+ *~
45
+
46
+ # OS files
47
+ .DS_Store
48
+ .DS_Store?
49
+ ._*
50
+ .Spotlight-V100
51
+ .Trashes
52
+ ehthumbs.db
53
+ Thumbs.db
54
+
55
+ # Logs
56
+ *.log
57
+ logs/
58
+
59
+ # Temporary files
60
+ *.tmp
61
+ *.temp
62
+ .researcher/
Dockerfile CHANGED
@@ -1,9 +1,27 @@
1
- FROM python:3.10.9
2
 
 
 
 
 
 
 
 
 
 
 
3
  COPY . .
4
 
5
- WORKDIR /
 
 
 
 
 
6
 
7
- RUN pip install --no-cache-dir --upgrade -r /requirements.txt
 
 
8
 
 
9
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ FROM python:3.10.9-slim
2
 
3
+ # Set working directory
4
+ WORKDIR /app
5
+
6
+ # Copy requirements first (for better caching)
7
+ COPY requirements.txt .
8
+
9
+ # Install dependencies
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Copy the application code
13
  COPY . .
14
 
15
+ # Create a non-root user for security
16
+ RUN adduser --disabled-password --gecos '' appuser && chown -R appuser:appuser /app
17
+ USER appuser
18
+
19
+ # Expose port 7860 (Hugging Face Spaces default)
20
+ EXPOSE 7860
21
 
22
+ # Set environment variables
23
+ ENV PYTHONPATH=/app
24
+ ENV PORT=7860
25
 
26
+ # Run the application
27
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
TextGen/__init__.py CHANGED
@@ -1,7 +1,12 @@
1
  from fastapi import FastAPI
2
 
3
- app = FastAPI(title="Deploying FastAPI Apps on Huggingface")
 
 
 
 
4
 
 
5
  from TextGen import router
6
 
7
 
 
1
  from fastapi import FastAPI
2
 
3
+ app = FastAPI(
4
+ title="FastAPI TextGen with OpenAI",
5
+ description="Simple FastAPI agent for answering questions using OpenAI",
6
+ version="1.0.0"
7
+ )
8
 
9
+ # Import router to register endpoints
10
  from TextGen import router
11
 
12
 
TextGen/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/TextGen/__pycache__/__init__.cpython-312.pyc and b/TextGen/__pycache__/__init__.cpython-312.pyc differ
 
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -1 +1,28 @@
1
- from TextGen import app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ FastAPI TextGen Application
4
+ Main entry point for the Hugging Face Space deployment
5
+ """
6
+
7
+ import os
8
+ import uvicorn
9
+ from TextGen import app
10
+
11
+ # This is required for Hugging Face Spaces
12
+ if __name__ == "__main__":
13
+ # Get port from environment variable (Hugging Face Spaces uses 7860)
14
+ port = int(os.environ.get("PORT", 7860))
15
+ host = os.environ.get("HOST", "0.0.0.0")
16
+
17
+ print(f"🚀 Starting FastAPI TextGen on {host}:{port}")
18
+
19
+ uvicorn.run(
20
+ "app:app",
21
+ host=host,
22
+ port=port,
23
+ reload=False,
24
+ log_level="info"
25
+ )
26
+
27
+ # Export the app instance for uvicorn when running directly
28
+ __all__ = ["app"]
.env → env_template.txt RENAMED
@@ -2,7 +2,8 @@
2
  # Copy this file to .env and fill in your actual values
3
 
4
  # OpenAI API Configuration
5
- OPENAI_API_KEY=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
 
6
  # Development Notes:
7
  # - Get your OpenAI API key from: https://platform.openai.com/api-keys
8
  # - Make sure your API key has sufficient credits
 
2
  # Copy this file to .env and fill in your actual values
3
 
4
  # OpenAI API Configuration
5
+ OPENAI_API_KEY=your_openai_api_key_here
6
+
7
  # Development Notes:
8
  # - Get your OpenAI API key from: https://platform.openai.com/api-keys
9
  # - Make sure your API key has sufficient credits
main.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Alternative main entry point for FastAPI TextGen
4
+ This file provides an alternative entry point that some deployment platforms prefer
5
+ """
6
+
7
+ from app import app
8
+
9
+ if __name__ == "__main__":
10
+ import uvicorn
11
+ import os
12
+
13
+ port = int(os.environ.get("PORT", 7860))
14
+ host = os.environ.get("HOST", "0.0.0.0")
15
+
16
+ uvicorn.run(
17
+ "main:app",
18
+ host=host,
19
+ port=port,
20
+ reload=False
21
+ )