Anshini commited on
Commit
0478706
·
verified ·
1 Parent(s): 7eaa0e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -196
app.py CHANGED
@@ -1,204 +1,61 @@
1
- import streamlit as st
2
- import os
3
- from langchain_huggingface import HuggingFaceEndpoint, HuggingFacePipeline, ChatHuggingFace
4
- from langchain_core.messages import HumanMessage,SystemMessage,AIMessage
5
-
6
- hf_token = os.getenv("HF_TOKEN")
7
-
8
-
9
- # Set the default page in session state
10
- if "page" not in st.session_state:
11
- st.session_state.page = "home"
12
-
13
- # Function to switch pages
14
- def switch_page(page_name):
15
- st.session_state.page = page_name
16
-
17
- # Home page with buttons for different domains
18
- if st.session_state.page == "home":
19
- st.title("🤖 Innomatics ChatGenius Hub")
20
- st.markdown("Choose a domain to chat with an expert model:")
21
-
22
- col1, col2, col3 = st.columns(3)
23
- with col1:
24
- if st.button("Python 🐍"):
25
- switch_page("python")
26
- if st.button("Statistics 📈"):
27
- switch_page("statistics")
28
-
29
-
30
-
31
- with col2:
32
- if st.button("SQL 🛢️"):
33
- switch_page("sql")
34
- if st.button("Machine Learning 🤖"):
35
- switch_page("ml")
36
-
37
-
38
- with col3:
39
- if st.button("Power BI 📊"):
40
- switch_page("powerbi")
41
- if st.button("Deep Learning 🧠"):
42
- switch_page("deeplearning")
43
- with col2:
44
- if st.button("GenAI🔮🤖"):
45
- switch_page("genai")
46
-
47
-
48
-
49
- # Example domain-specific chatbot page
50
- elif st.session_state.page == "python":
51
- st.title("Python Chatbot 🐍")
52
- # hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
53
- # if not hf_token:
54
- # st.error("Please add your Hugging Face API token to Secrets (HUGGINGFACEHUB_API_TOKEN or HF_TOKEN).")
55
- # st.stop()
56
-
57
- # # Setup the LangChain HuggingFaceEndpoint and ChatHuggingFace LLM
58
- # deep_seek_model = HuggingFaceEndpoint(
59
- # repo_id="deepseek-ai/DeepSeek-R1",
60
- # # provider = 'nebius'
61
- # temperature=0.7,
62
- # max_new_tokens=100,
63
- # task="conversational",
64
- # huggingfacehub_api_token=hf_token,
65
- # )
66
-
67
- # deepseek = ChatHuggingFace(
68
- # llm=deep_seek_model,
69
- # repo_id="deepseek-ai/DeepSeek-R1",
70
- # # provider="nebius",
71
- # temperature=0.7,
72
- # max_new_tokens=100,
73
- # task="conversational"
74
- # )
75
-
76
-
77
- gemma_model = HuggingFaceEndpoint(
78
- repo_id="google/gemma-3-27b-it",
79
- temperature=0.7,
80
- max_new_tokens=512,
81
- task="conversational",
82
- huggingfacehub_api_token=hf_token,
83
  )
84
-
85
- chat_gemma = ChatHuggingFace(
86
- llm=gemma_model,
87
- repo_id="google/gemma-3-27b-it",
88
- temperature=0.7,
89
- max_new_tokens=512,
90
- task="conversational",
 
 
 
 
 
 
 
91
  )
92
- # Initialize session state for chat history
93
- if "messages" not in st.session_state:
94
- st.session_state.messages = [
95
- SystemMessage(content="Answer like a 10 year experinced Python developer")
96
- ]
97
-
98
- def generate_response(user_input):
99
- # Append user message
100
- st.session_state.messages.append(HumanMessage(content=user_input))
101
- # Invoke the model
102
- response = chat_gemma.invoke(st.session_state.messages)
103
- # Append AI response
104
- st.session_state.messages.append(AIMessage(content=response))
105
- return response
106
-
107
- # User input
108
- user_input = st.text_input("Ask a question about Python:")
109
-
110
- if user_input:
111
- with st.spinner("Getting answer..."):
112
- answer = generate_response(user_input)
113
- st.markdown(f"**Answer:** {answer}")
114
-
115
- # Display chat history
116
- if st.session_state.messages:
117
- for msg in st.session_state.messages[1:]: # skip initial SystemMessage
118
- if isinstance(msg, HumanMessage):
119
- st.markdown(f"**You:** {msg.content}")
120
- elif isinstance(msg, AIMessage):
121
- st.markdown(f"**Bot:** {msg.content}")
122
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
123
- # Here you can load your Python LLM and chat interface
124
-
125
- elif st.session_state.page == "sql":
126
- st.title("SQL Chatbot 🛢️")
127
- if not hf_token:
128
- st.error("Please add your Hugging Face API token as an environment variable.")
129
- st.stop()
130
-
131
- # Initialize the LLaMA model from HuggingFace (via Nebius provider)
132
- llama_model = HuggingFaceEndpoint(
133
- repo_id="meta-llama/Llama-3.1-8B-Instruct",
134
- temperature=0.7,
135
- max_new_tokens=512,
136
- task="conversational",
137
- huggingfacehub_api_token=hf_token,
138
- )
139
-
140
- llama = ChatHuggingFace(
141
- llm=llama_model,
142
- repo_id="meta-llama/Llama-3.1-8B-Instruct",
143
- # provider="nebius",
144
- temperature=0.7,
145
- max_new_tokens=512,
146
- task="conversational"
147
- )
148
-
149
- # Streamlit A
150
-
151
- st.markdown("Ask anything related to SQL interviews!")
152
-
153
- # Initialize chat history
154
- if "messages" not in st.session_state:
155
- st.session_state.messages = [SystemMessage(content="Answer clearly like a technical 10 year experienced person in SQL .")]
156
-
157
- # User input
158
- user_input = st.text_input("💡 Ask your SQL interview question:", placeholder="e.g., give me 10 SQL interview questions with answers")
159
-
160
- def generate_response(user_input):
161
- st.session_state.messages.append(HumanMessage(content=user_input))
162
- response = llama.invoke(st.session_state.messages)
163
- st.session_state.messages.append(AIMessage(content=response))
164
- return response
165
-
166
- # Display response
167
- if user_input:
168
- with st.spinner("Thinking..."):
169
- answer = generate_response(user_input)
170
- st.markdown(f"**Answer:** {answer}")
171
-
172
- # Show chat history
173
- st.markdown("### 📜 Chat History")
174
- for msg in st.session_state.messages[1:]: # Skip SystemMessage
175
- if isinstance(msg, HumanMessage):
176
- st.markdown(f"**You:** {msg.content}")
177
- elif isinstance(msg, AIMessage):
178
- st.markdown(f"**Bot:** {msg.content}")
179
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
180
- # Load SQL chatbot here
181
-
182
- elif st.session_state.page == "powerbi":
183
- st.title("Power BI Chatbot 📊")
184
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
185
-
186
- elif st.session_state.page == "ml":
187
- st.title("Machine Learning Chatbot 🤖")
188
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
189
-
190
- elif st.session_state.page == "deeplearning":
191
- st.title("Deep Learning Chatbot 🧠")
192
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
193
 
194
- elif st.session_state.page == "statistics":
195
- st.title("Statistics Chatbot 📈")
196
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
 
 
 
197
 
198
- elif st.session_state.page == "genai":
199
- st.title("GenAI Chatbot 📈")
200
- st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
201
 
 
 
 
202
 
 
 
203
 
 
 
204
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_community.document_loaders import YoutubeLoader
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import torch
5
+
6
+ st.set_page_config(page_title="DeepSeek YouTube Summarizer", layout="centered")
7
+ st.title("📺 YouTube Video Summarizer with DeepSeek")
8
+
9
+ # Input YouTube URL
10
+ url = st.text_input("Enter YouTube Video URL:")
11
+
12
+ # Load DeepSeek model and tokenizer
13
+ @st.cache_resource
14
+ def load_model():
15
+ model_id = "deepseek-ai/deepseek-llm-7b-instruct"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
17
+ model = AutoModelForCausalLM.from_pretrained(
18
+ model_id, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
+ return tokenizer, model
21
+
22
+ tokenizer, model = load_model()
23
+
24
+ def summarize_with_deepseek(text):
25
+ prompt = f"""<|system|>\nYou are a helpful assistant.\n<|user|>\nSummarize the following text:\n{text}\n<|assistant|>"""
26
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
27
+ outputs = model.generate(
28
+ **inputs,
29
+ max_new_tokens=300,
30
+ do_sample=False,
31
+ temperature=0.7,
32
+ top_k=50,
33
+ top_p=0.95,
34
  )
35
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
+ return summary.split("<|assistant|>")[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ if st.button("Extract and Summarize"):
39
+ if url:
40
+ try:
41
+ loader = YoutubeLoader.from_youtube_url(url)
42
+ data = loader.load()
43
+ transcript = data[0].page_content if data else "No transcript found."
44
 
45
+ st.subheader("📖 Extracted Transcript")
46
+ st.text_area("Transcript:", transcript, height=300)
 
47
 
48
+ # Truncate for prompt length safety
49
+ if len(transcript) > 1500:
50
+ transcript = transcript[:1500]
51
 
52
+ with st.spinner("Summarizing using DeepSeek..."):
53
+ summary = summarize_with_deepseek(transcript)
54
 
55
+ st.subheader("🧠 Summary")
56
+ st.success(summary)
57
 
58
+ except Exception as e:
59
+ st.error(f"Error: {str(e)}")
60
+ else:
61
+ st.warning("Please enter a valid YouTube URL.")