Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| import google.generativeai as genai | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| # Get API key | |
| api_key = os.getenv("GEMINI_API_KEY") | |
| # If API key is not in environment variables, try to get it from Streamlit secrets | |
| if not api_key and "GEMINI_API_KEY" in st.secrets: | |
| api_key = st.secrets["GEMINI_API_KEY"] | |
| if not api_key: | |
| st.error("⚠️ Gemini API key not found. Please ensure GEMINI_API_KEY is set in environment variables or Streamlit secrets.") | |
| st.stop() | |
| try: | |
| genai.configure(api_key=api_key) | |
| except Exception as e: | |
| st.error(f"⚠️ Error configuring Gemini API: {str(e)}") | |
| st.stop() | |
| st.set_page_config(page_title="Gemini Stream Chat") | |
| st.markdown("## 🚀 AI replica for [Takeoff](https://readyfortakeoff.app/)") | |
| st.caption("Powered directly by `google.generativeai`") | |
| SYSTEM_PROMPT = """ | |
| You are an AI chatbot built for Takeoff (https://readyfortakeoff.app), a portfolio-building platform designed for individuals and jobseekers. | |
| 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. | |
| 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. | |
| Your goal is to make it easy for others to understand the user's background and professional strengths. | |
| """ | |
| #SYSTEM_PROMPT = """ | |
| #""" | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| if st.session_state.chat_history: | |
| for msg in st.session_state.chat_history: | |
| with st.chat_message(msg["role"]): | |
| st.markdown(msg["parts"][0]) | |
| prompt = st.chat_input("Feel free to ask me anything...") | |
| if prompt: | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| model = genai.GenerativeModel("gemini-1.5-flash") | |
| chat = model.start_chat(history=[ | |
| {"role": "user", "parts": [SYSTEM_PROMPT]}, | |
| {"role": "model", "parts": ["I understand. I will act as a helpful AI replica for Takeoff, focusing on providing professional and concise responses about the user's background and achievements."]}, | |
| *[ | |
| {"role": m["role"], "parts": [m["parts"][0]]} | |
| for m in st.session_state.chat_history | |
| ] | |
| ]) | |
| with st.chat_message("ai"): | |
| full_response = "" | |
| response_container = st.empty() | |
| response_stream = chat.send_message(prompt, stream=True) | |
| for chunk in response_stream: | |
| full_response += chunk.text | |
| response_container.markdown(full_response + "▌") | |
| response_container.markdown(full_response) | |
| st.session_state.chat_history.append({"role": "user", "parts": [prompt]}) | |
| st.session_state.chat_history.append({"role": "model", "parts": [full_response]}) | |