mherlie commited on
Commit
19c43bb
Β·
1 Parent(s): 1945e63

revised the UI

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -1,20 +1,20 @@
1
  import os
2
  import streamlit as st
3
- import google.generativeai as genai # βœ… Gemini API library
4
 
5
- # Load API key securely
6
  api_key = os.getenv("GEMINI_API_KEY", st.secrets.get("GEMINI_API_KEY"))
7
 
8
- # Check if API key is available
9
  if not api_key:
10
  st.error("⚠️ API key is missing! Set GEMINI_API_KEY as an environment variable or in Streamlit Secrets.")
11
  st.stop()
12
 
13
- # Configure Gemini API
14
  genai.configure(api_key=api_key)
15
 
16
- # Function to generate AI response
17
- def call_gemini_api(prompt, model, max_tokens, temperature):
18
  try:
19
  model = genai.GenerativeModel(model)
20
  response = model.generate_content(
@@ -24,44 +24,47 @@ def call_gemini_api(prompt, model, max_tokens, temperature):
24
  "temperature": temperature,
25
  },
26
  )
27
-
28
- # βœ… Extract only the generated text
29
  if response and response.candidates:
30
  return response.candidates[0].content.parts[0].text
31
  else:
32
  return "⚠️ No valid response from Gemini AI."
33
 
34
  except Exception as e:
35
- return f"Error: {str(e)}"
36
 
37
- # Configure Streamlit page
38
  st.set_page_config(page_title="Gemini AI Text Generator", layout="centered")
39
 
40
- # UI Elements
41
- st.title("πŸ“ Creative AI Assistant")
42
- st.write("Generate any helpful text with AI by providing a prompt below!")
43
 
44
- # User input fields
45
- user_input = st.text_area("✍️ Enter your prompt here:", height=150)
 
46
 
 
47
  col1, col2 = st.columns(2)
48
 
49
  with col1:
50
- model = st.selectbox("πŸ€– Choose AI Model:", ["gemini-pro", "gemini-pro-code"])
51
- max_tokens = st.slider("πŸ“ Max Tokens (Word Length)", min_value=50, max_value=1000, value=200, step=50)
52
 
53
  with col2:
54
- temperature = st.slider("🎨 Creativity Level (0 = Predictable, 1 = Creative)", min_value=0.0, max_value=1.0, value=0.7, step=0.1)
55
 
56
- # Generate button
 
57
  if st.button("πŸš€ Generate Text"):
58
  if user_input.strip():
59
- st.subheader("πŸ“Œ Generated Response:")
60
- result = call_gemini_api(user_input, model, max_tokens, temperature)
 
61
  st.write(result)
62
  else:
63
  st.warning("⚠️ Please enter a prompt before generating text.")
64
 
65
- # Footer
66
  st.markdown("---")
67
- st.caption("πŸ”Ή *Powered by Gemini AI* | πŸ”Ή *Developed with Streamlit*")
 
1
  import os
2
  import streamlit as st
3
+ import google.generativeai as genai
4
 
5
+ # βœ… Load API key securely
6
  api_key = os.getenv("GEMINI_API_KEY", st.secrets.get("GEMINI_API_KEY"))
7
 
8
+ # βœ… Check if API key is available
9
  if not api_key:
10
  st.error("⚠️ API key is missing! Set GEMINI_API_KEY as an environment variable or in Streamlit Secrets.")
11
  st.stop()
12
 
13
+ # βœ… Configure Gemini API
14
  genai.configure(api_key=api_key)
15
 
16
+ # βœ… Function to generate AI response
17
+ def generate_text(prompt, model, max_tokens, temperature):
18
  try:
19
  model = genai.GenerativeModel(model)
20
  response = model.generate_content(
 
24
  "temperature": temperature,
25
  },
26
  )
27
+ # βœ… Extract and return generated text
 
28
  if response and response.candidates:
29
  return response.candidates[0].content.parts[0].text
30
  else:
31
  return "⚠️ No valid response from Gemini AI."
32
 
33
  except Exception as e:
34
+ return f"❌ Error: {str(e)}"
35
 
36
+ # βœ… Streamlit Page Configuration
37
  st.set_page_config(page_title="Gemini AI Text Generator", layout="centered")
38
 
39
+ # βœ… UI Layout - Title & Description
40
+ st.title("πŸ€– Gemini AI-Powered Text Generator")
41
+ st.markdown("### Generate AI-powered text with ease. Simply enter a prompt and customize the settings!")
42
 
43
+ # βœ… User Input Fields
44
+ st.markdown("#### πŸ“ Enter Your Prompt")
45
+ user_input = st.text_area("Write something...", height=150, placeholder="Example: Write a formal article about climate change.")
46
 
47
+ # βœ… Layout for Options
48
  col1, col2 = st.columns(2)
49
 
50
  with col1:
51
+ model = st.selectbox("πŸ€– Choose AI Model", ["gemini-pro", "gemini-pro-code"])
52
+ max_tokens = st.slider("πŸ“ Max Tokens (Word Length)", min_value=50, max_value=1000, value=300, step=50)
53
 
54
  with col2:
55
+ temperature = st.slider("🎨 Creativity Level (1-10)", min_value=1, max_value=10, value=5) / 10 # Scaling to 0.1 - 1.0
56
 
57
+ # βœ… Generate Button
58
+ st.markdown("### ")
59
  if st.button("πŸš€ Generate Text"):
60
  if user_input.strip():
61
+ with st.spinner("✨ Generating response..."):
62
+ result = generate_text(user_input, model, max_tokens, temperature)
63
+ st.markdown("### 🎯 Generated Response")
64
  st.write(result)
65
  else:
66
  st.warning("⚠️ Please enter a prompt before generating text.")
67
 
68
+ # βœ… Footer
69
  st.markdown("---")
70
+ st.markdown("πŸ”Ή *Powered by Gemini AI* | πŸ”Ή *Developed with Streamlit*")