|
import streamlit as st |
|
import google.generativeai as genai |
|
import os |
|
import time |
|
|
|
|
|
st.set_page_config( |
|
page_title="Gemini Streaming Demo", |
|
page_icon="🤖", |
|
layout="wide" |
|
) |
|
|
|
|
|
st.title("🤖 Gemini API Streaming Demo") |
|
|
|
|
|
genai.configure(api_key=os.environ['GOOGLE_API_KEY']) |
|
|
|
|
|
model = genai.GenerativeModel('gemini-1.5-flash') |
|
|
|
|
|
user_input = st.text_area("Enter your prompt:", height=100) |
|
|
|
|
|
if st.button("Generate", type="primary"): |
|
if user_input: |
|
|
|
response_placeholder = st.empty() |
|
|
|
|
|
full_response = "" |
|
|
|
|
|
try: |
|
response = model.generate_content(user_input, stream=True) |
|
|
|
|
|
for chunk in response: |
|
if chunk.text: |
|
full_response += chunk.text |
|
|
|
response_placeholder.markdown(full_response + "▌") |
|
time.sleep(0.05) |
|
|
|
|
|
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.") |
|
|
|
|
|
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. |
|
""") |