Spaces:
Sleeping
Sleeping
deleted env
Browse files
.env
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
GPT4_API_KEY=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
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import openai
|
| 3 |
import os
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Streamlit UI
|
| 11 |
st.set_page_config(page_title="AI Poetry Translator", page_icon="π", layout="centered")
|
|
@@ -25,13 +33,15 @@ poetry_style = st.selectbox(
|
|
| 25 |
def generate_poetry(text, style):
|
| 26 |
try:
|
| 27 |
prompt = f"Convert the following text into {style} poetry:\n\n{text}\n\nPoem:"
|
| 28 |
-
response =
|
| 29 |
-
model="gpt-
|
| 30 |
-
messages=[
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
max_tokens=150
|
| 33 |
)
|
| 34 |
-
return response
|
| 35 |
except Exception as e:
|
| 36 |
return f"β Error generating poem: {str(e)}"
|
| 37 |
|
|
@@ -40,7 +50,7 @@ if st.button("Generate Poetry β¨"):
|
|
| 40 |
if text_input:
|
| 41 |
with st.spinner("Creating your poem... π"):
|
| 42 |
poem = generate_poetry(text_input, poetry_style)
|
| 43 |
-
if "β
|
| 44 |
st.error(poem)
|
| 45 |
else:
|
| 46 |
st.success("Hereβs your poetic masterpiece:")
|
|
|
|
| 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("OPENAI_API_KEY") # Use "OPENAI_API_KEY" (standard naming)
|
| 9 |
+
|
| 10 |
+
# Check if API key is set
|
| 11 |
+
if not api_key:
|
| 12 |
+
st.error("β Error: OpenAI API key is missing. Please check your .env file.")
|
| 13 |
+
st.stop()
|
| 14 |
+
|
| 15 |
+
# Initialize OpenAI client
|
| 16 |
+
client = OpenAI() # β
Correct initialization
|
| 17 |
|
| 18 |
# Streamlit UI
|
| 19 |
st.set_page_config(page_title="AI Poetry Translator", page_icon="π", layout="centered")
|
|
|
|
| 33 |
def generate_poetry(text, style):
|
| 34 |
try:
|
| 35 |
prompt = f"Convert the following text into {style} poetry:\n\n{text}\n\nPoem:"
|
| 36 |
+
response = client.chat.completions.create( # β
Correct syntax
|
| 37 |
+
model="gpt-3.5-turbo", # β
Free model
|
| 38 |
+
messages=[
|
| 39 |
+
{"role": "system", "content": "You are a skilled poet."},
|
| 40 |
+
{"role": "user", "content": prompt}
|
| 41 |
+
],
|
| 42 |
max_tokens=150
|
| 43 |
)
|
| 44 |
+
return response.choices[0].message.content.strip()
|
| 45 |
except Exception as e:
|
| 46 |
return f"β Error generating poem: {str(e)}"
|
| 47 |
|
|
|
|
| 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:")
|