File size: 1,916 Bytes
a1445fb 9ee01a9 0b9a8bb dd6e891 9ee01a9 0b9a8bb 9ee01a9 0b9a8bb 9ee01a9 0b9a8bb 9ee01a9 |
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 |
import streamlit as st
import google.generativeai as genai
import os
import time
# Setup page config
st.set_page_config(
page_title="Gemini Streaming Demo",
page_icon="🤖",
layout="wide"
)
# Create a title
st.title("🤖 Gemini API Streaming Demo")
# Configure the Gemini API with environment variable
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
# Initialize the model
model = genai.GenerativeModel('gemini-1.5-flash')
# Create input text area
user_input = st.text_area("Enter your prompt:", height=100)
# Create a button to generate content
if st.button("Generate", type="primary"):
if user_input:
# Create a placeholder for the streaming text
response_placeholder = st.empty()
# Initialize full_response
full_response = ""
# Generate streaming response
try:
response = model.generate_content(user_input, stream=True)
# Stream the response
for chunk in response:
if chunk.text:
full_response += chunk.text
# Update the placeholder with the full response so far
response_placeholder.markdown(full_response + "▌")
time.sleep(0.05) # Add a small delay for better visualization
# Final update without the cursor
response_placeholder.markdown(full_response)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
else:
st.warning("Please enter a prompt first.")
# Add instructions in the sidebar
with st.sidebar:
st.markdown("""
### Instructions
1. Type your prompt in the text area
2. Click 'Generate' to see the streaming response
### About
This demo shows how to use Gemini's streaming capabilities to generate text responses in real-time.
""") |