Michael Rey
modified code for HS secrets
973b88e
import streamlit as st
import os
import google.generativeai as genai
from dotenv import load_dotenv
# # Load API key from .env file
# load_dotenv()
# genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# Retrieve API key from Hugging Face Secrets
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
raise ValueError("API key not found. Make sure it is set in Hugging Face Secrets.")
# Configure Gemini API
genai.configure(api_key=api_key)
# Title of the app
st.title("KAI Chat")
st.markdown("Hi! I'm your Kool-AI Assistant! Start by entering text and adjust to generate text in your desired format, style, and length.")
# Input fields for user
core_topic = st.text_area("Enter your topic or text here:", height=150, placeholder="e.g., Climate change, AI advancements, etc.")
output_format = st.selectbox("Select the output format:", ["Story", "Poem", "Article", "Code"])
tone_style = st.selectbox("Choose the tone or style for the text:", ["Formal", "Informal", "Humorous", "Technical"])
length = st.slider("Choose the length of the generated text:", 50, 1000, 200, help="Shorter texts are ideal for quick summaries; longer ones for more detailed results.")
creativity = st.slider("Select creativity level (1 to 10):", 1, 10, 5, help="1 is less creative, 10 is highly creative.") / 10 # Scale down to 0.1 - 1.0
num_responses = st.radio("How many responses would you like to generate?", [1, 2, 3], help="Choose the number of different responses you'd like to receive.")
# Button for generating text
if st.button("Generate Text"):
if core_topic.strip() == "":
st.warning("Please enter a topic to get started!")
else:
try:
with st.spinner('Generating content...'):
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(
contents=f"Write a {tone_style.lower()} {output_format.lower()} about {core_topic}",
generation_config=genai.GenerationConfig(
max_output_tokens=length
)
)
# Display the generated content
st.subheader("Generated Text:")
st.write(response.text)
except Exception as e:
st.error(f"Oops! Something went wrong: {e}")