Mohammedelhakim commited on
Commit
ee05825
·
1 Parent(s): 73e1653
Files changed (4) hide show
  1. Dockerfile +12 -0
  2. app.env +1 -0
  3. app.py +92 -0
  4. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY main.py .
9
+
10
+ EXPOSE 7860
11
+
12
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY = "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"
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from fastapi import FastAPI, HTTPException
4
+ from pydantic import BaseModel
5
+ import requests as req
6
+ import pandas as pd
7
+ import chromadb
8
+ from sentence_transformers import SentenceTransformer
9
+ from huggingface_hub import snapshot_download
10
+ import os
11
+ from dotenv import load_dotenv
12
+
13
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
+ GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
15
+ GROQ_MODEL = "llama-3.1-8b-instant"
16
+ CHROMA_DIR = "chroma_db/chroma_db"
17
+ HF_DATASET_ID = "Mohammedelhakim/parenting-qa-vectordb"
18
+ NUM_RESULTS = 3
19
+
20
+
21
+ if not os.path.exists(CHROMA_DIR):
22
+ print("Downloading vector database from Hugging Face Hub...")
23
+ snapshot_download(
24
+ repo_id="Mohammedelhakim/parenting-qa-vectordb",
25
+ repo_type="dataset",
26
+ local_dir="chroma_db",
27
+
28
+ )
29
+ print("Vector database downloaded!")
30
+ else:
31
+ print("Vector database found locally, loading...")
32
+
33
+
34
+ print("Loading embedding model...")
35
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
36
+
37
+ chroma_client = chromadb.PersistentClient(path=CHROMA_DIR)
38
+ print(chroma_client.list_collections())
39
+ collection = chroma_client.get_collection("parenting_qa")
40
+ print("Ready!")
41
+
42
+
43
+ app = FastAPI()
44
+
45
+ class ChatRequest(BaseModel):
46
+ message: str
47
+
48
+ @app.post("/chat")
49
+ def chat(request: ChatRequest):
50
+ # 1. Embed the user question
51
+ query_embedding = embedder.encode(request.message).tolist()
52
+
53
+ # 2. Retrieve most similar Q&As
54
+ results = collection.query(
55
+ query_embeddings=[query_embedding],
56
+ n_results=NUM_RESULTS,
57
+ )
58
+
59
+ # 3. Build context
60
+ context = ""
61
+ for doc, meta in zip(results["documents"][0], results["metadatas"][0]):
62
+ context += f"Q: {doc}\nA: {meta['answer']}\n\n"
63
+
64
+ # 4. Call Groq
65
+ messages = [
66
+ {"role": "system", "content": (
67
+ "You are a helpful and caring parenting assistant. "
68
+ "You answer questions from parents about their babies and children, "
69
+ "such as health, development, feeding, sleep, and behavior. "
70
+ "Give clear, reassuring, and practical answers. "
71
+ "If something sounds medically serious, always advise the parent to consult a doctor. "
72
+ "Use the following similar Q&A examples to guide your answer:\n\n"
73
+ + context
74
+ )},
75
+ {"role": "user", "content": request.message},
76
+ ]
77
+
78
+ response = req.post(
79
+ GROQ_URL,
80
+ headers={
81
+ "Authorization": f"Bearer {GROQ_API_KEY}",
82
+ "Content-Type": "application/json",
83
+ },
84
+ json={"model": GROQ_MODEL, "messages": messages},
85
+ timeout=30,
86
+ )
87
+
88
+ result = response.json()
89
+ if "choices" not in result:
90
+ raise HTTPException(status_code=500, detail=f"Groq API error: {result}")
91
+
92
+ return {"response": result["choices"][0]["message"]["content"]}
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ requests
4
+ pandas
5
+ pyarrow
6
+ chromadb
7
+ sentence-transformers
8
+ huggingface_hub