muhammadhasnain100 Cursor commited on
Commit
8299bad
·
1 Parent(s): 49f22ac

Hugging Face Space: Docker image, hub token, CORS, Space README

Browse files
Files changed (5) hide show
  1. .dockerignore +11 -0
  2. Dockerfile +14 -4
  3. README.md +8 -7
  4. app.py +12 -1
  5. space_credentials.py +9 -0
.dockerignore ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .gitignore
3
+ **/__pycache__
4
+ **/*.py[cod]
5
+ .cursor
6
+ frontend/node_modules
7
+ frontend/.vite
8
+ *.md
9
+ !README.md
10
+ .env
11
+ .env.*
Dockerfile CHANGED
@@ -1,13 +1,23 @@
1
- FROM python:3.9
 
2
 
3
  RUN useradd -m -u 1000 user
4
  USER user
5
  ENV PATH="/home/user/.local/bin:$PATH"
 
 
 
 
 
 
6
 
7
  WORKDIR /app
8
 
9
- COPY --chown=user ./requirements.txt requirements.txt
10
- RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
11
 
12
  COPY --chown=user . /app
13
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
1
+ # Hugging Face Docker Space: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ FROM python:3.10-bookworm
3
 
4
  RUN useradd -m -u 1000 user
5
  USER user
6
  ENV PATH="/home/user/.local/bin:$PATH"
7
+ ENV PYTHONUNBUFFERED=1
8
+ ENV PYTHONDONTWRITEBYTECODE=1
9
+ # Cache heavy HF downloads on writable paths inside the container
10
+ ENV HF_HOME=/tmp/hf_home
11
+ ENV TRANSFORMERS_CACHE=/tmp/hf_home/transformers
12
+ ENV SENTENCE_TRANSFORMERS_HOME=/tmp/hf_home/sentence_transformers
13
 
14
  WORKDIR /app
15
 
16
+ COPY --chown=user ./requirements.txt ./requirements.txt
17
+ RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
18
+ && pip install --no-cache-dir --upgrade -r requirements.txt
19
 
20
  COPY --chown=user . /app
21
+
22
+ EXPOSE 7860
23
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,11 +1,12 @@
1
  ---
2
- title: Comsats Chatbot
3
- emoji: 🌍
4
- colorFrom: blue
5
- colorTo: yellow
6
  sdk: docker
7
- pinned: false
8
- short_description: This chatbot will answer your question related to CUI.
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: Backend
3
+ emoji: 🤖
4
+ colorFrom: gray
5
+ colorTo: blue
6
  sdk: docker
7
+ app_port: 7860
 
8
  ---
9
 
10
+ # COMSATS Chatbot — FastAPI backend
11
+
12
+ Docker Space running **FastAPI** + **Uvicorn** on port **7860**. Open `/docs` for Swagger.
app.py CHANGED
@@ -1,4 +1,7 @@
 
 
1
  from fastapi import FastAPI
 
2
  from langchain_groq import ChatGroq
3
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
4
 
@@ -10,6 +13,14 @@ app = FastAPI(
10
  swagger_ui_parameters={"persistAuthorization": True},
11
  )
12
 
 
 
 
 
 
 
 
 
13
  model_name = "BAAI/bge-small-en"
14
  model_kwargs = {"device": "cpu"}
15
  encode_kwargs = {"normalize_embeddings": True}
@@ -36,7 +47,7 @@ llm = ChatGroq(
36
  model_name="llama3-70b-8192",
37
  )
38
 
39
- paths = ["FYP Supervisor Feedback.csv", "urdu_data.csv", "english_data.csv"]
40
 
41
  chatbot = Comsatsbot(hf, llm, api_keys, chats_collection, paths, gemini_api_keys)
42
 
 
1
+ import space_credentials # noqa: F401 — sets HF hub env before embeddings load
2
+
3
  from fastapi import FastAPI
4
+ from fastapi.middleware.cors import CORSMiddleware
5
  from langchain_groq import ChatGroq
6
  from langchain_community.embeddings import HuggingFaceBgeEmbeddings
7
 
 
13
  swagger_ui_parameters={"persistAuthorization": True},
14
  )
15
 
16
+ app.add_middleware(
17
+ CORSMiddleware,
18
+ allow_origins=["*"],
19
+ allow_credentials=False,
20
+ allow_methods=["*"],
21
+ allow_headers=["*"],
22
+ )
23
+
24
  model_name = "BAAI/bge-small-en"
25
  model_kwargs = {"device": "cpu"}
26
  encode_kwargs = {"normalize_embeddings": True}
 
47
  model_name="llama3-70b-8192",
48
  )
49
 
50
+ paths = ["FYP Supervisor Feedback.csv", "urdu_data.csv", "english_data.csv", "csvv.csv"]
51
 
52
  chatbot = Comsatsbot(hf, llm, api_keys, chats_collection, paths, gemini_api_keys)
53
 
space_credentials.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ """Runtime credentials for Hugging Face Spaces (no .env). Import this before huggingface_hub loads."""
2
+
3
+ import os
4
+
5
+ # Hub token for embedding/model downloads (rw token from HF Space owner).
6
+ HF_TOKEN = ""
7
+
8
+ for _key in ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACEHUB_API_TOKEN"):
9
+ os.environ.setdefault(_key, HF_TOKEN)