Spaces:
Sleeping
Sleeping
changed api to gemini
Browse files- .env +1 -0
- app.py +23 -19
- requirements.txt +2 -1
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GEMINI_API_KEY=AIzaSyAkPli0DKS5bF8ugAw_6z-z_rdXpy2pMbI
|
app.py
CHANGED
|
@@ -1,23 +1,23 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import os
|
| 3 |
-
from openai import OpenAI
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
-
api_key = os.getenv("
|
| 9 |
|
| 10 |
# Check if API key is set
|
| 11 |
if not api_key:
|
| 12 |
-
st.error("β Error:
|
| 13 |
st.stop()
|
| 14 |
|
| 15 |
-
# Initialize
|
| 16 |
-
|
| 17 |
|
| 18 |
# Streamlit UI
|
| 19 |
-
st.set_page_config(page_title="AI Poetry Translator", page_icon="π", layout="centered")
|
| 20 |
-
st.title("π
|
| 21 |
st.write("Enter a phrase, sentence, or paragraph, and I'll transform it into poetry!")
|
| 22 |
|
| 23 |
# User input
|
|
@@ -29,19 +29,23 @@ poetry_style = st.selectbox(
|
|
| 29 |
["Shakespearean", "Haiku", "Free Verse", "Sonnet", "Limerick", "Rhyming Couplets"]
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Function to call
|
| 33 |
def generate_poetry(text, style):
|
| 34 |
try:
|
| 35 |
prompt = f"Convert the following text into {style} poetry:\n\n{text}\n\nPoem:"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"β Error generating poem: {str(e)}"
|
| 47 |
|
|
@@ -50,7 +54,7 @@ if st.button("Generate Poetry β¨"):
|
|
| 50 |
if text_input:
|
| 51 |
with st.spinner("Creating your poem... π"):
|
| 52 |
poem = generate_poetry(text_input, poetry_style)
|
| 53 |
-
if poem.startswith("β Error"):
|
| 54 |
st.error(poem)
|
| 55 |
else:
|
| 56 |
st.success("Hereβs your poetic masterpiece:")
|
|
@@ -61,4 +65,4 @@ if st.button("Generate Poetry β¨"):
|
|
| 61 |
|
| 62 |
# Footer
|
| 63 |
st.markdown("---")
|
| 64 |
-
st.write("π¨ Created with β€οΈ using
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
import os
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
+
api_key = os.getenv("GEMINI_API_KEY") # Use Google Gemini API key
|
| 9 |
|
| 10 |
# Check if API key is set
|
| 11 |
if not api_key:
|
| 12 |
+
st.error("β Error: Google API key is missing. Please check your .env file.")
|
| 13 |
st.stop()
|
| 14 |
|
| 15 |
+
# Initialize Google Gemini AI client
|
| 16 |
+
genai.configure(api_key=api_key)
|
| 17 |
|
| 18 |
# Streamlit UI
|
| 19 |
+
st.set_page_config(page_title="REYREY's AI Poetry Translator", page_icon="π", layout="centered")
|
| 20 |
+
st.title("π REYREY's Poetry Translator π")
|
| 21 |
st.write("Enter a phrase, sentence, or paragraph, and I'll transform it into poetry!")
|
| 22 |
|
| 23 |
# User input
|
|
|
|
| 29 |
["Shakespearean", "Haiku", "Free Verse", "Sonnet", "Limerick", "Rhyming Couplets"]
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Function to call Google Gemini API
|
| 33 |
def generate_poetry(text, style):
|
| 34 |
try:
|
| 35 |
prompt = f"Convert the following text into {style} poetry:\n\n{text}\n\nPoem:"
|
| 36 |
+
|
| 37 |
+
model = genai.GenerativeModel("gemini-pro") # Use Google's Gemini Pro model
|
| 38 |
+
response = model.generate_content(prompt)
|
| 39 |
+
|
| 40 |
+
# β
Check if response has valid candidates
|
| 41 |
+
if response.candidates:
|
| 42 |
+
for candidate in response.candidates:
|
| 43 |
+
if candidate.finish_reason == "STOP":
|
| 44 |
+
return candidate.content.parts[0].text.strip()
|
| 45 |
+
|
| 46 |
+
# β
Handle safety filter blocking content
|
| 47 |
+
return "β AI Response was blocked due to content safety filters."
|
| 48 |
+
|
| 49 |
except Exception as e:
|
| 50 |
return f"β Error generating poem: {str(e)}"
|
| 51 |
|
|
|
|
| 54 |
if text_input:
|
| 55 |
with st.spinner("Creating your poem... π"):
|
| 56 |
poem = generate_poetry(text_input, poetry_style)
|
| 57 |
+
if poem.startswith("β Error") or poem.startswith("β AI Response"):
|
| 58 |
st.error(poem)
|
| 59 |
else:
|
| 60 |
st.success("Hereβs your poetic masterpiece:")
|
|
|
|
| 65 |
|
| 66 |
# Footer
|
| 67 |
st.markdown("---")
|
| 68 |
+
st.write("π¨ Created with β€οΈ using Google Gemini & Streamlit")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
streamlit
|
| 2 |
openai>=1.0.0
|
| 3 |
-
python-dotenv
|
|
|
|
|
|
| 1 |
streamlit
|
| 2 |
openai>=1.0.0
|
| 3 |
+
python-dotenv
|
| 4 |
+
google-generativeai
|