File size: 2,327 Bytes
1b74fd0
 
a3f2217
1b74fd0
a3f2217
1b74fd0
 
a3f2217
1b74fd0
 
 
 
a3f2217
1b74fd0
 
a3f2217
 
1b74fd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3f2217
1b74fd0
a3f2217
1b74fd0
 
a3f2217
5d7277f
a3f2217
1b74fd0
a3f2217
 
1b74fd0
a3f2217
 
 
1b74fd0
a3f2217
 
1b74fd0
a3f2217
1b74fd0
 
a3f2217
 
 
1b74fd0
 
 
 
a3f2217
1b74fd0
55381a3
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
64
65
import os
import streamlit as st
import google.generativeai as genai  

# βœ… Load API key securely
api_key = os.getenv("GEMINI_API_KEY", st.secrets.get("GEMINI_API_KEY"))

# βœ… Check if API key is available
if not api_key:
    st.error("⚠️ API key is missing! Set GEMINI_API_KEY as an environment variable or in Streamlit Secrets.")
    st.stop()

# βœ… Configure Gemini API
genai.configure(api_key=api_key)

# βœ… Function to generate AI response
def generate_text(prompt, model, max_tokens, temperature):
    try:
        model = genai.GenerativeModel(model)
        response = model.generate_content(
            prompt,
            generation_config={
                "max_output_tokens": max_tokens,
                "temperature": temperature,
            },
        )
        if response and response.candidates:
            return response.candidates[0].content.parts[0].text
        else:
            return "⚠️ No valid response from Gemini AI."
    
    except Exception as e:
        return f"❌ Error: {str(e)}"

# βœ… Streamlit Page Configuration
st.set_page_config(page_title="Gemini AI Text Generator", layout="centered")

# βœ… Title & Description
st.title("Creative AI Assistant")
st.write("Generate AI-powered text by entering a prompt and customizing the settings.")

# βœ… Input Prompt
user_input = st.text_area("πŸ“ Enter Your Prompt:", height=150, placeholder="Example: Write a formal article about climate change.")

# βœ… Sliders (Aligned Below Prompt)
temperature = st.slider("🎨 Creativity Level (1-10)", min_value=1, max_value=10, value=5) / 10  
max_tokens = st.slider("πŸ“ Max Tokens (Word Length)", min_value=50, max_value=1000, value=300, step=50)

# βœ… AI Model Selection (Below Sliders)
model = st.selectbox("πŸ€– Choose AI Model", ["gemini-pro", "gemini-pro-code"])

# βœ… Generate Button (Below Model Selection)
if st.button("πŸš€ Generate Text"):
    if user_input.strip():
        with st.spinner("✨ Generating response..."):
            result = generate_text(user_input, model, max_tokens, temperature)
        st.subheader("🎯 Generated Response")
        st.write(result)
    else:
        st.warning("⚠️ Please enter a prompt before generating text.")

# βœ… Footer
st.markdown("---")
st.caption("πŸ”Ή *Powered by Gemini AI* | πŸ”Ή *Developed with Streamlit*")