Spaces:
Runtime error
Runtime error
Model Changed
Browse files
app.py
CHANGED
|
@@ -1,74 +1,83 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from
|
| 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 |
-
with
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
#
|
| 65 |
-
if
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Blog Generation Using Google Gemini",
|
| 11 |
+
layout="centered",
|
| 12 |
+
initial_sidebar_state="collapsed"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Configure the Google Gemini API
|
| 16 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
| 17 |
+
genai.configure(api_key=api_key)
|
| 18 |
+
|
| 19 |
+
# Function to list available models
|
| 20 |
+
def list_available_models():
|
| 21 |
+
try:
|
| 22 |
+
models = genai.list_models()
|
| 23 |
+
return [model.name for model in models if "generateContent" in model.supported_generation_methods]
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Error listing models: {e}")
|
| 26 |
+
return []
|
| 27 |
+
|
| 28 |
+
# Generate blog content using Gemini
|
| 29 |
+
def generate_blog(keywords, no_words, blog_style):
|
| 30 |
+
try:
|
| 31 |
+
available_models = list_available_models()
|
| 32 |
+
|
| 33 |
+
if not available_models:
|
| 34 |
+
return "Unable to retrieve available models. Please check your API key and network connection."
|
| 35 |
+
|
| 36 |
+
# Select the first available generative model
|
| 37 |
+
model_name = available_models[6]
|
| 38 |
+
model = genai.GenerativeModel(model_name)
|
| 39 |
+
|
| 40 |
+
prompt = f"Generate a blog for {blog_style} job profile based on the keywords '{keywords}' within {no_words} words."
|
| 41 |
+
|
| 42 |
+
response = model.generate_content(prompt)
|
| 43 |
+
return response.text
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error generating content: {e}"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
st.header("Blog Generation App :earth_americas:")
|
| 50 |
+
st.write("This app uses Google's Gemini AI for generating blogs. Please provide your inputs below:")
|
| 51 |
+
|
| 52 |
+
# Input fields
|
| 53 |
+
keywords = st.text_input("Enter the Blog Keywords")
|
| 54 |
+
col1, col2 = st.columns([5, 5])
|
| 55 |
+
with col1:
|
| 56 |
+
no_words = st.text_input("Number of Words")
|
| 57 |
+
with col2:
|
| 58 |
+
blog_style = st.selectbox(
|
| 59 |
+
"Writing the blog for",
|
| 60 |
+
["Researchers", "Engineers", "Doctors", "Content Creators", "Sportsman", "Businessman", "Common People"],
|
| 61 |
+
index=0
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Display available models
|
| 65 |
+
# if st.checkbox("Show available models"):
|
| 66 |
+
# models = list_available_models()
|
| 67 |
+
# if models:
|
| 68 |
+
# st.write("Available models:")
|
| 69 |
+
# for model in models:
|
| 70 |
+
# st.write(f"- {model}")
|
| 71 |
+
# else:
|
| 72 |
+
# st.write("No models available or unable to retrieve models.")
|
| 73 |
+
|
| 74 |
+
submit = st.button("Generate Blog")
|
| 75 |
+
|
| 76 |
+
# Generate response
|
| 77 |
+
if submit:
|
| 78 |
+
if keywords and no_words.isdigit() and int(no_words) > 0:
|
| 79 |
+
st.write("Generating blog...")
|
| 80 |
+
response = generate_blog(keywords, no_words, blog_style)
|
| 81 |
+
st.markdown(f"### Generated Blog:\n\n{response}")
|
| 82 |
+
else:
|
| 83 |
+
st.warning("Please provide valid inputs for all fields.")
|