Spaces:
Sleeping
Sleeping
add code
Browse files- src/streamlit_app.py +50 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,56 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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]})
|