Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ from google.genai.types import GenerateContentConfig
|
|
7 |
from datasets import load_dataset
|
8 |
from huggingface_hub import login
|
9 |
from typing import List, Dict, Any
|
|
|
|
|
|
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
@@ -16,10 +19,23 @@ google_api_key = os.environ.get("GOOGLE_API_KEY")
|
|
16 |
|
17 |
login(token=hf_token)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
class ChatRequest(BaseModel):
|
25 |
message: str
|
@@ -33,28 +49,57 @@ class ChatRequest(BaseModel):
|
|
33 |
chat_history: List[Dict[str, Any]] = []
|
34 |
model_choice: str = "google"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
@app.post("/chat")
|
37 |
async def chat(request: ChatRequest):
|
38 |
try:
|
39 |
if request.model_choice == "google":
|
40 |
client = genai.Client(api_key=google_api_key)
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
if len(request.chat_history) > summary_thresh:
|
45 |
-
summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
46 |
-
unnecessary details and provide a clear, straightforward summary. {request.chat_history[:-summary_thresh]}""" # summarize everything except last k items
|
47 |
-
summary_response = client.models.generate_content(
|
48 |
-
model="gemini-2.0-flash",
|
49 |
-
contents=summarize_prompt,
|
50 |
-
config=GenerateContentConfig(
|
51 |
-
system_instruction=["You are a helpful assistant who is an expert at summarization."]
|
52 |
-
),
|
53 |
-
)
|
54 |
-
request.chat_history = request.chat_history[-(summary_thresh+1):] # keep last k items
|
55 |
-
request.chat_history.insert(0, {"role": "user", "parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
56 |
|
|
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
response = client.models.generate_content(
|
59 |
model="gemini-2.0-flash",
|
60 |
contents=request.chat_history,
|
@@ -62,7 +107,11 @@ async def chat(request: ChatRequest):
|
|
62 |
system_instruction=[request.system_message]
|
63 |
),
|
64 |
)
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
if request.model_choice == "HF":
|
68 |
if hf_token:
|
|
|
7 |
from datasets import load_dataset
|
8 |
from huggingface_hub import login
|
9 |
from typing import List, Dict, Any
|
10 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
12 |
+
from langchain.vectorstores import FAISS
|
13 |
|
14 |
app = FastAPI()
|
15 |
|
|
|
19 |
|
20 |
login(token=hf_token)
|
21 |
|
22 |
+
def chunk_text(text, chunk_size=50, chunk_overlap=10):
|
23 |
+
splitter = RecursiveCharacterTextSplitter(
|
24 |
+
chunk_size=chunk_size, chunk_overlap=chunk_overlap, separators=[" ", "\n"]
|
25 |
+
)
|
26 |
+
chunks = splitter.split_text(text)
|
27 |
+
return chunks
|
28 |
+
|
29 |
+
# Function to build FAISS index
|
30 |
+
def build_faiss_vectorstore(chunks):
|
31 |
+
vectorstore = FAISS.from_texts(chunks, embedding_model)
|
32 |
+
return vectorstore
|
33 |
+
|
34 |
+
# Function to retrieve similar text
|
35 |
+
def retrieve(query, vectorstore, top_k=5):
|
36 |
+
docs = vectorstore.similarity_search(query, k=top_k)
|
37 |
+
return [doc.page_content for doc in docs]
|
38 |
+
|
39 |
|
40 |
class ChatRequest(BaseModel):
|
41 |
message: str
|
|
|
49 |
chat_history: List[Dict[str, Any]] = []
|
50 |
model_choice: str = "google"
|
51 |
|
52 |
+
|
53 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
54 |
+
|
55 |
+
# grab dataset
|
56 |
+
dataset = load_dataset("Lhumpal/youtube-hunting-beast-transcripts", data_files={"concise": "concise/*", "raw": "raw/*"})
|
57 |
+
concise_text = dataset["concise"]["text"]
|
58 |
+
concise_text_string = "".join(concise_text)
|
59 |
+
|
60 |
+
# Chunk and index the documents
|
61 |
+
chunks = chunk_text(concise_text_string, chunk_size=30))
|
62 |
+
# Build the vectorsore
|
63 |
+
vectorstore = build_faiss_vectorstore(chunks)
|
64 |
+
|
65 |
@app.post("/chat")
|
66 |
async def chat(request: ChatRequest):
|
67 |
try:
|
68 |
if request.model_choice == "google":
|
69 |
client = genai.Client(api_key=google_api_key)
|
70 |
|
71 |
+
# Retrieve relevant text
|
72 |
+
results = retrieve(request.message, vectorstore)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
rag_prompt = f"""You have access to the following relevant information retrieved based on the user's query:
|
75 |
|
76 |
+
{"\n\n".join(results)}
|
77 |
+
|
78 |
+
Using the information above, answer the user's query as accurately as possible:
|
79 |
+
|
80 |
+
User's Query: {request.message}
|
81 |
+
"""
|
82 |
+
|
83 |
+
# # summarize chat history
|
84 |
+
# summary_thresh = 2
|
85 |
+
# if len(request.chat_history) > summary_thresh:
|
86 |
+
# summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
87 |
+
# unnecessary details and provide a clear, straightforward summary. {request.chat_history[:-summary_thresh]}""" # summarize everything except last k items
|
88 |
+
# summary_response = client.models.generate_content(
|
89 |
+
# model="gemini-2.0-flash",
|
90 |
+
# contents=summarize_prompt,
|
91 |
+
# config=GenerateContentConfig(
|
92 |
+
# system_instruction=["You are a helpful assistant who is an expert at summarization."]
|
93 |
+
# ),
|
94 |
+
# )
|
95 |
+
# request.chat_history = request.chat_history[-(summary_thresh+1):] # keep last k items
|
96 |
+
# request.chat_history.insert(0, {"role": "user", "parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
97 |
+
|
98 |
+
# remove the unfformatted user message
|
99 |
+
del request.chat_history[-1]
|
100 |
+
# add the user message with RAG data
|
101 |
+
request.chat_history.append({"role": "user", "parts": [{"text": rag_prompt}]})
|
102 |
+
|
103 |
response = client.models.generate_content(
|
104 |
model="gemini-2.0-flash",
|
105 |
contents=request.chat_history,
|
|
|
107 |
system_instruction=[request.system_message]
|
108 |
),
|
109 |
)
|
110 |
+
|
111 |
+
# put back the unformatted user message
|
112 |
+
request.chat_history.append({"role": "user", "parts": [{"text": request.message}]})
|
113 |
+
|
114 |
+
return {"response": response.text, "dataset_str": concise_text_string[:75], "history": request.chat_history, "RAG_prompt": rag_prompt_template}
|
115 |
|
116 |
if request.model_choice == "HF":
|
117 |
if hf_token:
|