Spaces:
Runtime error
Runtime error
import os | |
import streamlit as st | |
import google.generativeai as genai | |
# API key | |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" | |
# Configure the API key | |
genai.configure(api_key=api_key) | |
# Create chatbot interface | |
st.title("Gemini API Chatbot") | |
# Get chat history from session state | |
chat_history = st.session_state.get("chat_history", []) | |
# Get user input from text box | |
user_input = st.text_input("You") | |
# Check if user input is not empty | |
if user_input: | |
# Add user message to chat history | |
chat_history.append(user_input) | |
# Display user message with markdown | |
st.markdown(f"**You:** {user_input}") | |
# Create model object | |
model = genai.GenerativeModel(model_name="gemini-pro") | |
# Get model response with generate_content method | |
with st.spinner("Thinking..."): | |
response = model.generate_content(chat_history) | |
# Get response text from response object | |
response_text = response.contents[-1].parts[0].text | |
# Add response message to chat history | |
chat_history.append(response_text) | |
# Display response message with markdown | |
st.markdown(f"**Gemini Bot:** {response_text}") | |
# Update session state with chat history | |
st.session_state["chat_history"] = chat_history | |