julianubc commited on
Commit
05ee5fd
·
1 Parent(s): 18df92c
Files changed (1) hide show
  1. src/streamlit_app.py +50 -34
src/streamlit_app.py CHANGED
@@ -1,40 +1,56 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
 
 
 
 
 
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
 
 
14
  """
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
1
+ import os
 
 
2
  import streamlit as st
3
+ import google.generativeai as genai
4
+ from dotenv import load_dotenv
5
 
6
+ load_dotenv()
7
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
8
+
9
+ st.set_page_config(page_title="Gemini Stream Chat")
10
+ st.markdown("## 🚀 AI replica for [Takeoff](https://readyfortakeoff.app/)")
11
+ st.caption("Powered directly by `google.generativeai`")
12
+
13
+ SYSTEM_PROMPT = """
14
+ You are an AI chatbot built for Takeoff (https://readyfortakeoff.app), a portfolio-building platform designed for individuals and jobseekers.
15
 
16
+ Your role is to act as a helpful AI replica embedded in a user's portfolio. You can answer questions from recruiters and visitors about the user's work experience, projects, and skills. You should highlight relevant examples and provide helpful, professional, and concise responses.
 
 
17
 
18
+ You can reference data such as the user's resume, portfolio content, project notes, and achievements. Where appropriate, link to projects or suggest relevant content the user has created.
19
+
20
+ Your goal is to make it easy for others to understand the user's background and professional strengths.
21
  """
22
 
23
+ if "chat_history" not in st.session_state:
24
+ st.session_state.chat_history = []
25
+
26
+ if st.session_state.chat_history:
27
+ for msg in st.session_state.chat_history:
28
+ with st.chat_message(msg["role"]):
29
+ st.markdown(msg["parts"][0])
30
+
31
+ prompt = st.chat_input("Feel free to ask me anything...")
32
+
33
+ if prompt:
34
+ with st.chat_message("user"):
35
+ st.markdown(prompt)
36
+
37
+ model = genai.GenerativeModel("gemini-1.5-flash")
38
+ chat = model.start_chat(history=[
39
+ {"role": m["role"], "parts": [m["parts"][0]]}
40
+ for m in st.session_state.chat_history
41
+ ])
42
+
43
+ full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {prompt}"
44
+
45
+ with st.chat_message("ai"):
46
+ full_response = ""
47
+ response_container = st.empty()
48
+
49
+ response_stream = chat.send_message(full_prompt, stream=True)
50
+ for chunk in response_stream:
51
+ full_response += chunk.text
52
+ response_container.markdown(full_response + "▌")
53
+ response_container.markdown(full_response)
54
+
55
+ st.session_state.chat_history.append({"role": "user", "parts": [prompt]})
56
+ st.session_state.chat_history.append({"role": "model", "parts": [full_response]})