Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.llms import HuggingFacePipeline
|
| 7 |
-
from transformers import
|
| 8 |
import torch
|
| 9 |
import os
|
| 10 |
import requests
|
|
@@ -53,16 +53,19 @@ def create_vector_store(transcript):
|
|
| 53 |
return FAISS.from_documents(docs, embeddings)
|
| 54 |
|
| 55 |
|
| 56 |
-
# π€ Load Free
|
| 57 |
@st.cache_resource
|
| 58 |
-
def
|
| 59 |
-
model_name = "
|
| 60 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 61 |
-
model =
|
| 62 |
pipe = pipeline(
|
| 63 |
-
"
|
| 64 |
model=model,
|
| 65 |
tokenizer=tokenizer,
|
|
|
|
|
|
|
|
|
|
| 66 |
device=0 if torch.cuda.is_available() else -1
|
| 67 |
)
|
| 68 |
return HuggingFacePipeline(pipeline=pipe)
|
|
@@ -72,26 +75,33 @@ def load_bloom():
|
|
| 72 |
def build_model(model_choice, temperature=0.7):
|
| 73 |
if model_choice == "DeepSeek":
|
| 74 |
repo_id = "deepseek-ai/DeepSeek-V3.2-Exp" # paid
|
| 75 |
-
llm = HuggingFaceEndpoint(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return ChatHuggingFace(llm=llm, temperature=temperature)
|
| 77 |
elif model_choice == "OpenAI":
|
| 78 |
repo_id = "openai/gpt-oss-20b" # paid
|
| 79 |
-
llm = HuggingFaceEndpoint(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return ChatHuggingFace(llm=llm, temperature=temperature)
|
| 81 |
else:
|
| 82 |
-
return
|
| 83 |
|
| 84 |
|
| 85 |
-
# π§Ύ Prompt Template
|
| 86 |
prompt_template = PromptTemplate(
|
| 87 |
template=(
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"If both the context and your knowledge are insufficient, say: 'I don't know.'\n\n"
|
| 93 |
-
"Context:\n{context}\n\n"
|
| 94 |
-
"Question:\n{question}"
|
| 95 |
),
|
| 96 |
input_variables=["context", "question"]
|
| 97 |
)
|
|
@@ -102,7 +112,7 @@ st.title("π₯ YouTube Transcript Chatbot (Hybrid: Free + Paid)")
|
|
| 102 |
|
| 103 |
video_id = st.text_input("YouTube Video ID", value="lv1_-RER4_I")
|
| 104 |
query = st.text_area("Your Query", value="What is RAG?")
|
| 105 |
-
model_choice = st.radio("Model to Use", ["
|
| 106 |
temperature = st.slider("Temperature", 0, 100, value=50) / 100.0
|
| 107 |
|
| 108 |
if st.button("π Run Chatbot"):
|
|
@@ -117,11 +127,21 @@ if st.button("π Run Chatbot"):
|
|
| 117 |
st.success(f"β
Transcript fetched! ({len(transcript)} characters)")
|
| 118 |
|
| 119 |
with st.spinner("Generating response..."):
|
| 120 |
-
retriever = create_vector_store(transcript).as_retriever(
|
|
|
|
|
|
|
|
|
|
| 121 |
relevant_docs = retriever.invoke(query)
|
| 122 |
context_text = "\n\n".join(doc.page_content for doc in relevant_docs)
|
| 123 |
prompt = prompt_template.format(context=context_text, question=query)
|
| 124 |
|
| 125 |
model = build_model(model_choice, temperature)
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.llms import HuggingFacePipeline
|
| 7 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 8 |
import torch
|
| 9 |
import os
|
| 10 |
import requests
|
|
|
|
| 53 |
return FAISS.from_documents(docs, embeddings)
|
| 54 |
|
| 55 |
|
| 56 |
+
# π€ Load Free Flan-T5 locally (Better than BLOOM)
|
| 57 |
@st.cache_resource
|
| 58 |
+
def load_flan_t5():
|
| 59 |
+
model_name = "google/flan-t5-base"
|
| 60 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 61 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 62 |
pipe = pipeline(
|
| 63 |
+
"text2text-generation",
|
| 64 |
model=model,
|
| 65 |
tokenizer=tokenizer,
|
| 66 |
+
max_length=512,
|
| 67 |
+
temperature=0.7,
|
| 68 |
+
do_sample=True,
|
| 69 |
device=0 if torch.cuda.is_available() else -1
|
| 70 |
)
|
| 71 |
return HuggingFacePipeline(pipeline=pipe)
|
|
|
|
| 75 |
def build_model(model_choice, temperature=0.7):
|
| 76 |
if model_choice == "DeepSeek":
|
| 77 |
repo_id = "deepseek-ai/DeepSeek-V3.2-Exp" # paid
|
| 78 |
+
llm = HuggingFaceEndpoint(
|
| 79 |
+
repo_id=repo_id,
|
| 80 |
+
huggingfacehub_api_token=api_key,
|
| 81 |
+
task="text-generation",
|
| 82 |
+
max_new_tokens=500
|
| 83 |
+
)
|
| 84 |
return ChatHuggingFace(llm=llm, temperature=temperature)
|
| 85 |
elif model_choice == "OpenAI":
|
| 86 |
repo_id = "openai/gpt-oss-20b" # paid
|
| 87 |
+
llm = HuggingFaceEndpoint(
|
| 88 |
+
repo_id=repo_id,
|
| 89 |
+
huggingfacehub_api_token=api_key,
|
| 90 |
+
task="text-generation",
|
| 91 |
+
max_new_tokens=500
|
| 92 |
+
)
|
| 93 |
return ChatHuggingFace(llm=llm, temperature=temperature)
|
| 94 |
else:
|
| 95 |
+
return load_flan_t5() # free local Flan-T5
|
| 96 |
|
| 97 |
|
| 98 |
+
# π§Ύ Prompt Template (Simplified for T5)
|
| 99 |
prompt_template = PromptTemplate(
|
| 100 |
template=(
|
| 101 |
+
"Answer the question based on the context below.\n\n"
|
| 102 |
+
"Context: {context}\n\n"
|
| 103 |
+
"Question: {question}\n\n"
|
| 104 |
+
"Answer:"
|
|
|
|
|
|
|
|
|
|
| 105 |
),
|
| 106 |
input_variables=["context", "question"]
|
| 107 |
)
|
|
|
|
| 112 |
|
| 113 |
video_id = st.text_input("YouTube Video ID", value="lv1_-RER4_I")
|
| 114 |
query = st.text_area("Your Query", value="What is RAG?")
|
| 115 |
+
model_choice = st.radio("Model to Use", ["Flan-T5 (Free)", "DeepSeek", "OpenAI"])
|
| 116 |
temperature = st.slider("Temperature", 0, 100, value=50) / 100.0
|
| 117 |
|
| 118 |
if st.button("π Run Chatbot"):
|
|
|
|
| 127 |
st.success(f"β
Transcript fetched! ({len(transcript)} characters)")
|
| 128 |
|
| 129 |
with st.spinner("Generating response..."):
|
| 130 |
+
retriever = create_vector_store(transcript).as_retriever(
|
| 131 |
+
search_type="mmr",
|
| 132 |
+
search_kwargs={"k": 5}
|
| 133 |
+
)
|
| 134 |
relevant_docs = retriever.invoke(query)
|
| 135 |
context_text = "\n\n".join(doc.page_content for doc in relevant_docs)
|
| 136 |
prompt = prompt_template.format(context=context_text, question=query)
|
| 137 |
|
| 138 |
model = build_model(model_choice, temperature)
|
| 139 |
+
|
| 140 |
+
# Handle response based on model type
|
| 141 |
+
if model_choice == "Flan-T5 (Free)":
|
| 142 |
+
response = model(prompt)
|
| 143 |
+
else:
|
| 144 |
+
response_obj = model.invoke(prompt)
|
| 145 |
+
response = response_obj.content if hasattr(response_obj, 'content') else str(response_obj)
|
| 146 |
+
|
| 147 |
+
st.text_area("Model Response", value=response, height=400)
|