-
Notifications
You must be signed in to change notification settings - Fork 129
/
main.py
132 lines (107 loc) · 3.63 KB
/
main.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
from dotenv import load_dotenv
load_dotenv()
from typing import Set
import streamlit as st
from streamlit_chat import message
from backend.core import run_llm
st.set_page_config(
page_title="Your App Title",
page_icon="🧊",
layout="wide",
initial_sidebar_state="expanded",
)
# Add these imports
from PIL import Image
import requests
from io import BytesIO
def create_sources_string(source_urls: Set[str]) -> str:
if not source_urls:
return ""
sources_list = list(source_urls)
sources_list.sort()
sources_string = "sources:\n"
for i, source in enumerate(sources_list):
sources_string += f"{i+1}. {source}\n"
return sources_string
# Add this function to get a profile picture
def get_profile_picture(email):
# This uses Gravatar to get a profile picture based on email
# You can replace this with a different service or use a default image
gravatar_url = f"https://www.gravatar.com/avatar/{hash(email)}?d=identicon&s=200"
response = requests.get(gravatar_url)
img = Image.open(BytesIO(response.content))
return img
# Custom CSS for dark theme and modern look
st.markdown(
"""
<style>
.stApp {
background-color: #1E1E1E;
color: #FFFFFF;
}
.stTextInput > div > div > input {
background-color: #2D2D2D;
color: #FFFFFF;
}
.stButton > button {
background-color: #4CAF50;
color: #FFFFFF;
}
.stSidebar {
background-color: #252526;
}
.stMessage {
background-color: #2D2D2D;
}
</style>
""",
unsafe_allow_html=True,
)
# Set page config at the very beginning
# Sidebar user information
with st.sidebar:
st.title("User Profile")
# You can replace these with actual user data
user_name = "John Doe"
user_email = "[email protected]"
profile_pic = get_profile_picture(user_email)
st.image(profile_pic, width=150)
st.write(f"**Name:** {user_name}")
st.write(f"**Email:** {user_email}")
st.header("LangChain🦜🔗 Udemy Course- Helper Bot")
# Initialize session state
if "chat_answers_history" not in st.session_state:
st.session_state["chat_answers_history"] = []
st.session_state["user_prompt_history"] = []
st.session_state["chat_history"] = []
# Create two columns for a more modern layout
col1, col2 = st.columns([2, 1])
with col1:
prompt = st.text_input("Prompt", placeholder="Enter your message here...")
with col2:
if st.button("Submit", key="submit"):
prompt = prompt or "Hello" # Default message if input is empty
if prompt:
with st.spinner("Generating response..."):
generated_response = run_llm(
query=prompt, chat_history=st.session_state["chat_history"]
)
sources = set(doc.metadata["source"] for doc in generated_response["context"])
formatted_response = (
f"{generated_response['answer']} \n\n {create_sources_string(sources)}"
)
st.session_state["user_prompt_history"].append(prompt)
st.session_state["chat_answers_history"].append(formatted_response)
st.session_state["chat_history"].append(("human", prompt))
st.session_state["chat_history"].append(("ai", generated_response["answer"]))
# Display chat history
if st.session_state["chat_answers_history"]:
for generated_response, user_query in zip(
st.session_state["chat_answers_history"],
st.session_state["user_prompt_history"],
):
message(user_query, is_user=True, key=f"user_{user_query}")
message(generated_response, key=f"bot_{generated_response}")
# Add a footer
st.markdown("---")
st.markdown("Powered by LangChain and Streamlit")