Nada commited on
Commit
cddf446
·
1 Parent(s): 24c77f5
Files changed (3) hide show
  1. app.py +8 -8
  2. hf_spaces.py +34 -0
  3. requirements.txt +27 -26
app.py CHANGED
@@ -19,23 +19,22 @@ app = FastAPI(
19
  version="1.0.0"
20
  )
21
 
22
- allowed_origins = os.getenv("ALLOWED_ORIGINS", "http://localhost:8000").split(",")
23
-
24
- # Add CORS middleware
25
  app.add_middleware(
26
  CORSMiddleware,
27
- allow_origins=allowed_origins,
28
  allow_credentials=True,
29
  allow_methods=["*"],
30
  allow_headers=["*"],
31
  )
32
 
33
- # Initialize chatbot
34
  chatbot = MentalHealthChatbot(
35
  model_name=os.getenv("MODEL_NAME", "meta-llama/Llama-3.2-3B-Instruct"),
36
- peft_model_path=os.getenv("PEFT_MODEL_PATH", "llama_fine_tuned"),
37
  therapy_guidelines_path=os.getenv("GUIDELINES_PATH", "guidelines.txt"),
38
- use_4bit=True
 
39
  )
40
 
41
  # pydantic models
@@ -194,4 +193,5 @@ async def get_user_replies(user_id: str):
194
 
195
  if __name__ == "__main__":
196
  import uvicorn
197
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
19
  version="1.0.0"
20
  )
21
 
22
+ # Add CORS middleware - allow all origins for Hugging Face Spaces
 
 
23
  app.add_middleware(
24
  CORSMiddleware,
25
+ allow_origins=["*"], # Allow all origins in Hugging Face Spaces
26
  allow_credentials=True,
27
  allow_methods=["*"],
28
  allow_headers=["*"],
29
  )
30
 
31
+ # Initialize chatbot with Hugging Face Spaces specific settings
32
  chatbot = MentalHealthChatbot(
33
  model_name=os.getenv("MODEL_NAME", "meta-llama/Llama-3.2-3B-Instruct"),
34
+ peft_model_path=os.getenv("PEFT_MODEL_PATH", "nada013/mental-health-chatbot"),
35
  therapy_guidelines_path=os.getenv("GUIDELINES_PATH", "guidelines.txt"),
36
+ use_4bit=True,
37
+ device="cpu" # Force CPU for Hugging Face Spaces
38
  )
39
 
40
  # pydantic models
 
193
 
194
  if __name__ == "__main__":
195
  import uvicorn
196
+ # Use port 7860 for Hugging Face Spaces
197
+ uvicorn.run(app, host="0.0.0.0", port=7860)
hf_spaces.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Hugging Face Spaces specific configuration
3
+ """
4
+ import os
5
+
6
+ # Set environment variables for Hugging Face Spaces
7
+ os.environ.update({
8
+ 'TRANSFORMERS_CACHE': '/tmp/huggingface',
9
+ 'HF_HOME': '/tmp/huggingface',
10
+ 'TOKENIZERS_PARALLELISM': 'false',
11
+ 'TRANSFORMERS_VERBOSITY': 'error',
12
+ 'BITSANDBYTES_NOWELCOME': '1'
13
+ })
14
+
15
+ # Create necessary directories
16
+ for directory in ['/tmp/huggingface', '/tmp/vector_db', '/tmp/session_data', '/tmp/session_summaries']:
17
+ os.makedirs(directory, exist_ok=True)
18
+
19
+ # Hugging Face Spaces specific settings
20
+ SPACES_CONFIG = {
21
+ 'port': 7860, # Default port for Hugging Face Spaces
22
+ 'host': '0.0.0.0',
23
+ 'workers': 1, # Single worker for Hugging Face Spaces
24
+ 'timeout': 120, # Increased timeout for model loading
25
+ 'log_level': 'info'
26
+ }
27
+
28
+ # Model settings optimized for Hugging Face Spaces
29
+ MODEL_CONFIG = {
30
+ 'model_name': 'meta-llama/Llama-3.2-3B-Instruct',
31
+ 'peft_model_path': 'nada013/mental-health-chatbot',
32
+ 'use_4bit': True,
33
+ 'device': 'cpu' # Force CPU for Hugging Face Spaces
34
+ }
requirements.txt CHANGED
@@ -1,26 +1,27 @@
1
- transformers==4.49.0
2
- torch==2.2.0
3
- sentence-transformers==3.4.1
4
- langchain==0.3.21
5
- langchain-community==0.3.20
6
- langchain-core==0.3.47
7
- langchain-huggingface==0.1.2
8
- pydantic==2.10.6
9
- pydantic-settings==2.8.1
10
- fastapi==0.115.11
11
- uvicorn==0.34.0
12
- python-dotenv==1.0.1
13
- pytest==7.4.0
14
- gunicorn==21.2.0
15
- accelerate==1.5.2
16
- bitsandbytes==0.45.3
17
- chromadb==0.6.3
18
- datasets==3.4.1
19
- faiss-cpu==1.10.0
20
- huggingface-hub==0.29.3
21
- peft==0.15.1
22
- safetensors==0.5.3
23
- tokenizers==0.21.1
24
- tiktoken==0.9.0
25
- starlette==0.46.1
26
- websockets==15.0.1
 
 
1
+ transformers>=4.49.0
2
+ torch>=2.2.0
3
+ sentence-transformers>=3.4.1
4
+ langchain>=0.3.21
5
+ langchain-community>=0.3.20
6
+ langchain-core>=0.3.47
7
+ langchain-huggingface>=0.1.2
8
+ pydantic>=2.10.6
9
+ pydantic-settings>=2.8.1
10
+ fastapi>=0.115.11
11
+ uvicorn>=0.34.0
12
+ python-dotenv>=1.0.1
13
+ pytest>=7.4.0
14
+ gunicorn>=21.2.0
15
+ accelerate>=1.5.2
16
+ bitsandbytes>=0.45.3
17
+ chromadb>=0.6.3
18
+ datasets>=3.4.1
19
+ faiss-cpu>=1.10.0
20
+ huggingface-hub>=0.29.3
21
+ peft>=0.15.1
22
+ safetensors>=0.5.3
23
+ tokenizers>=0.21.1
24
+ tiktoken>=0.9.0
25
+ starlette>=0.46.1
26
+ websockets>=15.0.1
27
+ python-multipart>=0.0.6