Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
import openai | |
from PIL import Image | |
import requests | |
# Set your OpenAI API key | |
openai.api_key = "OPENAI_API_KEY" # Replace with your OpenAI API key | |
# Load predefined quotes dataset (Here, using a static example for now) | |
random_text = "The only way to do great work is to love what you do." | |
random_author = "Steve Jobs" | |
# Load user-submitted quotes | |
if "user_quotes" not in st.session_state: | |
st.session_state.user_quotes = [] | |
# Set up Streamlit page | |
st.set_page_config(page_title="Quotation of the Day", page_icon="π", layout="centered") | |
st.title("π Quotation of the Day") | |
# Show a predefined quote | |
st.write(f"π£οΈ *{random_text}* β {random_author}") | |
# AI-Generated Quote Image | |
st.subheader("πΌοΈ AI-Generated Quote Image") | |
if st.button("Generate Quote Image"): | |
# Call OpenAI's DALLΒ·E model to generate an image from the quote | |
response = openai.Image.create( | |
prompt=random_text, # Use the random quote as the prompt | |
n=1, # Generate 1 image | |
size="1024x1024" # Image size | |
) | |
# Extract the image URL from the response | |
image_url = response['data'][0]['url'] | |
# Display the image | |
st.image(image_url, caption="AI-Generated Quote Image") | |
# AI-Generated Quote (Using GPT-3) | |
st.subheader("π‘ Get an AI-Generated Quote") | |
if st.button("Generate AI Quote"): | |
# Call OpenAI's GPT-3 model to generate a quote | |
response = openai.Completion.create( | |
engine="text-davinci-003", # Choose the GPT-3 model | |
prompt="A great quote is", # Starting text for the quote | |
max_tokens=50 # Limit the number of tokens (words/characters) | |
) | |
# Get the generated quote | |
ai_quote = response.choices[0].text.strip() | |
# Display the AI-generated quote | |
st.write(f"β¨ *{ai_quote}* β AI") | |
# User Authentication with Hugging Face (In this case, weβre not using it) | |
st.subheader("π Submit Your Own Quote") | |
if "username" not in st.session_state: | |
username = st.text_input("Enter your name (for quote submission)") | |
if st.button("Submit"): | |
st.session_state.username = username | |
st.success(f"β Logged in as {username}") | |
# Submit Your Own Quote | |
if "username" in st.session_state: | |
st.subheader("π Submit Your Own Quote") | |
user_quote = st.text_area("Enter your quote") | |
if st.button("Submit Quote"): | |
if user_quote.strip(): | |
st.session_state.user_quotes.append({'quote': user_quote, 'author': st.session_state.username}) | |
st.success("β Quote added successfully!") | |
else: | |
st.error("β Please enter a quote!") | |
# Display community quotes (user-submitted quotes) | |
st.subheader("π Community Quotes") | |
for i, q in enumerate(st.session_state.user_quotes): | |
col1, col2 = st.columns([4, 1]) | |
with col1: | |
st.write(f"π¬ *{q['quote']}* β {q['author']}") | |
with col2: | |
if st.button(f"Upvote {i}", key=f"upvote_{i}"): | |
st.session_state.user_quotes[i]['upvotes'] = st.session_state.user_quotes.get('upvotes', 0) + 1 | |
st.experimental_rerun() | |