Daksh0505 commited on
Commit
2b0fa65
Β·
verified Β·
1 Parent(s): aa7e489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
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 AutoModelForCausalLM, AutoTokenizer, pipeline
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 BLOOM locally
57
  @st.cache_resource
58
- def load_bloom():
59
- model_name = "bigscience/bloom-560m"
60
  tokenizer = AutoTokenizer.from_pretrained(model_name)
61
- model = AutoModelForCausalLM.from_pretrained(model_name)
62
  pipe = pipeline(
63
- "text-generation",
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(repo_id=repo_id, huggingfacehub_api_token=api_key, task="text-generation")
 
 
 
 
 
76
  return ChatHuggingFace(llm=llm, temperature=temperature)
77
  elif model_choice == "OpenAI":
78
  repo_id = "openai/gpt-oss-20b" # paid
79
- llm = HuggingFaceEndpoint(repo_id=repo_id, huggingfacehub_api_token=api_key, task="text-generation")
 
 
 
 
 
80
  return ChatHuggingFace(llm=llm, temperature=temperature)
81
  else:
82
- return load_bloom() # free local BLOOM
83
 
84
 
85
- # 🧾 Prompt Template
86
  prompt_template = PromptTemplate(
87
  template=(
88
- "You are a helpful assistant.\n\n"
89
- "Answer the question using the context provided below.\n"
90
- "If the context does not mention the topic, say clearly: 'There is no mention of the topic in the video you provided.'\n"
91
- "Then, based on your own knowledge, try to answer the question.\n"
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", ["DeepSeek", "OpenAI", "Free BLOOM"])
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(search_type="mmr", search_kwargs={"k": 5})
 
 
 
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
- response = model.invoke(prompt) if model_choice != "Free BLOOM" else model(prompt)
127
- st.text_area("Model Response", value=response if isinstance(response, str) else response.content, height=400)
 
 
 
 
 
 
 
 
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)