generated from streamlit/app-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit-app.py
184 lines (153 loc) · 6.85 KB
/
streamlit-app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import json
import yt_dlp
import streamlit as st
import whisper
from hugchat import hugchat
from hugchat.login import Login
from prepromps import preprompts
# Define the cookie path directory
COOKIE_PATH_DIR = "./cookies/"
COOKIE_FILE_PATH = os.path.join(COOKIE_PATH_DIR, "cookies.json")
# Ensure the cookie directory exists
if not os.path.exists(COOKIE_PATH_DIR):
os.makedirs(COOKIE_PATH_DIR)
model = whisper.load_model("base")
def authenticate(email: str, password: str):
"""
Authenticate user and return cookies.
"""
sign = Login(email, password)
cookies = sign.login(cookie_dir_path=COOKIE_PATH_DIR, save_cookies=True)
return cookies.get_dict()
def load_cookies():
"""
Load cookies from file if they exist.
"""
if os.path.exists(COOKIE_FILE_PATH):
with open(COOKIE_FILE_PATH, 'r') as file:
cookies = json.load(file)
return cookies
return None
def save_cookies(cookies):
"""
Save cookies to file.
"""
with open(COOKIE_FILE_PATH, 'w') as file:
json.dump(cookies, file)
def youtube_downloader(link, dir_path):
"""
Download audio from a YouTube link.
"""
with yt_dlp.YoutubeDL({'extract_audio': True, 'format': 'bestaudio', 'outtmpl': os.path.join(dir_path, '%(id)s.wav')}) as audio:
info_dict = audio.extract_info(link, download=True)
id = info_dict['id']
return os.path.join(dir_path, str(id) + ".wav")
def main():
if 'chatbot' not in st.session_state:
st.title("Personal assistant")
st.subheader("Accessing personal assistant using HuggingChat")
# Sidebar for user authentication
st.sidebar.header("User Authentication")
# Load cookies if available
cookies = load_cookies()
if cookies:
st.session_state.cookies = cookies
st.session_state.is_authenticated = True
st.sidebar.success("Loaded cookies. You are connected.")
else:
email = st.sidebar.text_input("Email", value="", type="default")
password = st.sidebar.text_input("Password", value="", type="password")
if st.sidebar.button("Connect"):
if email and password:
# Authenticate and store cookies
cookies = authenticate(email, password)
st.session_state.cookies = cookies
st.session_state.is_authenticated = True
save_cookies(cookies)
st.sidebar.success("Successfully connected!")
else:
st.error("Please provide both email and password.")
# Check if cookies are available and user is authenticated
if 'cookies' in st.session_state and st.session_state.is_authenticated:
cookies = st.session_state.cookies
else:
st.warning("Please connect with your credentials first.")
return
# Pre-prompt setting
st.sidebar.subheader('Assistants')
st.sidebar.markdown(
'Select an assistant to get expert help tailored to your needs. Each assistant is here to provide specialized support and guidance.')
# Assistant type selection
st.session_state.assistant_type = st.sidebar.selectbox(
"Select an Assistant:",
preprompts.keys()
)
st.session_state.allow_web_search = st.sidebar.checkbox(
"Allow web search")
# Audio download option
if st.session_state.assistant_type == "Youtube Summary":
youtube_link = st.sidebar.text_input("YouTube Link for Audio")
if st.sidebar.button("Process Link", use_container_width=True):
if youtube_link:
with st.spinner("processing video..."):
audio_file = youtube_downloader(
youtube_link, COOKIE_PATH_DIR)
result = model.transcribe(audio_file)
result = result['text']
st.session_state.TTS_result = result
st.sidebar.success(f"Video processed")
else:
st.sidebar.error("Please provide a valid YouTube link.")
else:
st.session_state.TTS_result = ""
ini_assistant = st.sidebar.button(
"Initialize Assistant", type="primary", use_container_width=True)
# Create ChatBot instance
if ini_assistant:
if st.session_state.assistant_type != "Youtube Summary":
with st.spinner("Initializing Chatbot..."):
if 'chatbot' not in st.session_state:
st.session_state.chatbot = hugchat.ChatBot(cookies=cookies)
print(st.session_state.chatbot.chat(
preprompts[st.session_state.assistant_type][0] + " If it is ok for you respond 'OK!'", web_search=st.session_state.allow_web_search))
if st.session_state.assistant_type == "Youtube Summary":
with st.spinner("Initializing Chatbot..."):
if 'chatbot' not in st.session_state:
st.session_state.chatbot = hugchat.ChatBot(cookies=cookies)
response = st.session_state.chatbot.chat(
preprompts[st.session_state.assistant_type][0] + "\n" + st.session_state.TTS_result + "\n Now, return the summaries please.", web_search=st.session_state.allow_web_search)
preprompts[st.session_state.assistant_type][1] = response
if 'chatbot' in st.session_state:
st.image(preprompts[st.session_state.assistant_type][2])
# Chat interface
# Store LLM generated responses
if "messages" not in st.session_state.keys():
st.session_state.messages = [
{"role": "assistant", "content": preprompts[st.session_state.assistant_type][1]}]
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User-provided prompt
if prompt := st.chat_input(disabled=not st.session_state.is_authenticated):
st.session_state.messages.append(
{"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
try:
with st.spinner("Thinking..."):
response = st.session_state.chatbot.chat(
prompt, web_search=st.session_state.allow_web_search)
st.markdown(response)
except:
st.markdown(
"**ERROR**: The server is crowded, try again...")
message = {"role": "assistant", "content": response}
st.session_state.messages.append(message)
if __name__ == "__main__":
st.set_page_config(page_title="Personal assistant", page_icon="🤗")
main()