Spaces:
Sleeping
Sleeping
syedMohib44
commited on
Commit
Β·
6a03bb0
1
Parent(s):
d5b5047
- .gitignore +34 -0
- Dockerfile +21 -0
- app.py +109 -0
- dataset/pentagon_core.json +8 -0
- requirements.txt +8 -0
- space.yaml +3 -0
.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*.so
|
5 |
+
|
6 |
+
# Virtual environment
|
7 |
+
venv/
|
8 |
+
env/
|
9 |
+
.venv/
|
10 |
+
|
11 |
+
# Build
|
12 |
+
build/
|
13 |
+
dist/
|
14 |
+
|
15 |
+
# Jupyter Notebook checkpoints
|
16 |
+
.ipynb_checkpoints/
|
17 |
+
|
18 |
+
# Logs and local environment files
|
19 |
+
*.log
|
20 |
+
*.env
|
21 |
+
.env.local
|
22 |
+
|
23 |
+
# PyTorch or TensorFlow saved models
|
24 |
+
*.pt
|
25 |
+
*.pth
|
26 |
+
*.h5
|
27 |
+
|
28 |
+
# VSCode settings (if using VSCode)
|
29 |
+
.vscode/
|
30 |
+
|
31 |
+
# Hugging Face cache (optional)
|
32 |
+
/content/huggingface/
|
33 |
+
# dataset/
|
34 |
+
discord/
|
Dockerfile
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
RUN apt-get update && apt-get install -y git git-lfs wget unzip && rm -rf /var/lib/apt/lists/*
|
4 |
+
COPY requirements.txt .
|
5 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
COPY app.py .
|
9 |
+
COPY dataset ./dataset
|
10 |
+
|
11 |
+
# Hugging Face cache fix
|
12 |
+
ENV TRANSFORMERS_CACHE=/app/models/.cache
|
13 |
+
|
14 |
+
# Clone models
|
15 |
+
RUN git lfs install && \
|
16 |
+
git clone https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 /app/models/all-MiniLM-L6-v2 && \
|
17 |
+
git clone https://huggingface.co/facebook/opt-1.3b /app/models/facebook-opt-1.3b && \
|
18 |
+
git clone https://huggingface.co/facebook/bart-large-cnn /app/models/bart-large-cnn
|
19 |
+
|
20 |
+
EXPOSE 7860
|
21 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
from fastapi import FastAPI
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
from pydantic import BaseModel
|
6 |
+
from typing import List
|
7 |
+
from transformers import pipeline
|
8 |
+
from sentence_transformers import SentenceTransformer
|
9 |
+
import faiss
|
10 |
+
import gradio as gr
|
11 |
+
from gradio import mount_gradio_app
|
12 |
+
|
13 |
+
# ------------------- Config ------------------- #
|
14 |
+
DATA_PATH = "./dataset/pentagon_core.json"
|
15 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
16 |
+
QA_MODEL = "facebook/bart-large-cnn"
|
17 |
+
DEVICE = "cuda" if os.environ.get("USE_CUDA") == "1" else "cpu"
|
18 |
+
|
19 |
+
# ------------------- Load Models ------------------- #
|
20 |
+
embedder = SentenceTransformer(EMBEDDING_MODEL)
|
21 |
+
qa_model = pipeline("text2text-generation", model=QA_MODEL, device=0 if DEVICE == "cuda" else -1)
|
22 |
+
|
23 |
+
# ------------------- Load Dataset + Index ------------------- #
|
24 |
+
if os.path.exists(DATA_PATH):
|
25 |
+
with open(DATA_PATH, "r") as f:
|
26 |
+
knowledge_base = json.load(f)
|
27 |
+
else:
|
28 |
+
knowledge_base = []
|
29 |
+
|
30 |
+
texts = [item["content"] for item in knowledge_base]
|
31 |
+
embeddings = embedder.encode(texts, convert_to_tensor=True)
|
32 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
33 |
+
index.add(embeddings.cpu().detach().numpy())
|
34 |
+
|
35 |
+
# ------------------- FastAPI App ------------------- #
|
36 |
+
app = FastAPI()
|
37 |
+
app.add_middleware(
|
38 |
+
CORSMiddleware,
|
39 |
+
allow_origins=["*"], # For development
|
40 |
+
allow_credentials=True,
|
41 |
+
allow_methods=["*"],
|
42 |
+
allow_headers=["*"],
|
43 |
+
)
|
44 |
+
|
45 |
+
# --------- Upload Endpoint --------- #
|
46 |
+
class UploadData(BaseModel):
|
47 |
+
content: str
|
48 |
+
|
49 |
+
@app.post("/upload/")
|
50 |
+
def upload_knowledge(data: UploadData):
|
51 |
+
global knowledge_base, index
|
52 |
+
|
53 |
+
knowledge_base.append({"content": data.content})
|
54 |
+
with open(DATA_PATH, "w") as f:
|
55 |
+
json.dump(knowledge_base, f, indent=2)
|
56 |
+
|
57 |
+
new_embedding = embedder.encode([data.content], convert_to_numpy=True)
|
58 |
+
index.add(new_embedding)
|
59 |
+
|
60 |
+
return {"message": "Data uploaded and indexed."}
|
61 |
+
|
62 |
+
# --------- Ask Endpoint --------- #
|
63 |
+
@app.get("/ask/")
|
64 |
+
def ask(question: str, top_k: int = 3):
|
65 |
+
question_embedding = embedder.encode([question], convert_to_numpy=True)
|
66 |
+
distances, indices = index.search(question_embedding, top_k)
|
67 |
+
context = " ".join([knowledge_base[i]["content"] for i in indices[0]])
|
68 |
+
|
69 |
+
prompt = (
|
70 |
+
f"Context: {context}\n\n"
|
71 |
+
f"Answer the following question based only on the above context:\n"
|
72 |
+
f"{question}\n\nAnswer:"
|
73 |
+
)
|
74 |
+
output = qa_model(prompt, max_length=256, do_sample=False)[0]["generated_text"]
|
75 |
+
|
76 |
+
return {
|
77 |
+
"question": question,
|
78 |
+
"context_used": context,
|
79 |
+
"answer": output.strip()
|
80 |
+
}
|
81 |
+
|
82 |
+
# --------- Gradio UI --------- #
|
83 |
+
def gradio_upload(file):
|
84 |
+
if file is None:
|
85 |
+
return "No file selected."
|
86 |
+
|
87 |
+
try:
|
88 |
+
content = file.read().decode("utf-8")
|
89 |
+
import requests
|
90 |
+
|
91 |
+
base_url = os.getenv("HF_SPACE_URL", "http://localhost:7860")
|
92 |
+
response = requests.post(f"{base_url}/upload/", json={"content": content})
|
93 |
+
|
94 |
+
if response.status_code == 200:
|
95 |
+
return "β
Data successfully uploaded and indexed!"
|
96 |
+
else:
|
97 |
+
return f"β Failed: {response.text}"
|
98 |
+
except Exception as e:
|
99 |
+
return f"β Error: {str(e)}"
|
100 |
+
|
101 |
+
gr_app = gr.Interface(
|
102 |
+
fn=gradio_upload,
|
103 |
+
inputs=gr.File(label="Upload .txt or .json file"),
|
104 |
+
outputs="text",
|
105 |
+
title="Upload Knowledge",
|
106 |
+
)
|
107 |
+
|
108 |
+
# Mount Gradio at /ui
|
109 |
+
app = mount_gradio_app(app, gr_app, path="/ui")
|
dataset/pentagon_core.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"content": "This is the first knowledge piece."
|
4 |
+
},
|
5 |
+
{
|
6 |
+
"content": "This is the second knowledge piece."
|
7 |
+
}
|
8 |
+
]
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
gradio
|
4 |
+
transformers
|
5 |
+
sentence-transformers
|
6 |
+
faiss-cpu
|
7 |
+
torch
|
8 |
+
python-pptx
|
space.yaml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
title: Test-api Space
|
2 |
+
sdk: docker
|
3 |
+
app_port: 7860
|