Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,21 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
-
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
from dataset import load_user_quotes, save_user_quote, upvote_quote
|
| 6 |
from PIL import Image
|
| 7 |
import requests
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
# Load user-submitted quotes
|
| 16 |
if "user_quotes" not in st.session_state:
|
| 17 |
-
st.session_state.user_quotes =
|
| 18 |
-
|
| 19 |
-
# Load AI quote generator (GPT-2)
|
| 20 |
-
quote_generator = pipeline("text-generation", model="gpt2")
|
| 21 |
|
|
|
|
| 22 |
st.set_page_config(page_title="Quotation of the Day", page_icon="π", layout="centered")
|
| 23 |
st.title("π Quotation of the Day")
|
| 24 |
|
|
@@ -28,26 +25,40 @@ st.write(f"π£οΈ *{random_text}* β {random_author}")
|
|
| 28 |
# AI-Generated Quote Image
|
| 29 |
st.subheader("πΌοΈ AI-Generated Quote Image")
|
| 30 |
if st.button("Generate Quote Image"):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# AI-Generated Quote
|
| 41 |
st.subheader("π‘ Get an AI-Generated Quote")
|
| 42 |
if st.button("Generate AI Quote"):
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
st.write(f"β¨ *{ai_quote}* β AI")
|
| 45 |
|
| 46 |
-
# User Authentication with Hugging Face
|
| 47 |
-
st.subheader("π
|
| 48 |
if "username" not in st.session_state:
|
| 49 |
-
username = st.text_input("Enter
|
| 50 |
-
if st.button("
|
| 51 |
st.session_state.username = username
|
| 52 |
st.success(f"β
Logged in as {username}")
|
| 53 |
|
|
@@ -55,25 +66,21 @@ if "username" not in st.session_state:
|
|
| 55 |
if "username" in st.session_state:
|
| 56 |
st.subheader("π Submit Your Own Quote")
|
| 57 |
user_quote = st.text_area("Enter your quote")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if st.button("Submit"):
|
| 61 |
if user_quote.strip():
|
| 62 |
-
|
| 63 |
-
st.session_state.user_quotes = load_user_quotes() # Update session state
|
| 64 |
st.success("β
Quote added successfully!")
|
| 65 |
-
st.rerun() # Use st.rerun() instead of st.experimental_rerun()
|
| 66 |
else:
|
| 67 |
st.error("β Please enter a quote!")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
st.subheader("π Community Quotes")
|
| 71 |
-
for i, q in enumerate(st.session_state.user_quotes):
|
| 72 |
col1, col2 = st.columns([4, 1])
|
| 73 |
with col1:
|
| 74 |
-
st.write(f"π¬ *{q['quote']}* β {q['author']}
|
| 75 |
with col2:
|
| 76 |
-
if st.button(f"Upvote {i}", key=f"upvote_{i}"):
|
| 77 |
-
|
| 78 |
-
st.
|
| 79 |
-
st.rerun() # Use st.rerun() here as well
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
+
import openai
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
|
| 7 |
+
# Set your OpenAI API key
|
| 8 |
+
openai.api_key = "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" # Replace with your OpenAI API key
|
| 9 |
+
|
| 10 |
+
# Load predefined quotes dataset (Here, using a static example for now)
|
| 11 |
+
random_text = "The only way to do great work is to love what you do."
|
| 12 |
+
random_author = "Steve Jobs"
|
| 13 |
|
| 14 |
# Load user-submitted quotes
|
| 15 |
if "user_quotes" not in st.session_state:
|
| 16 |
+
st.session_state.user_quotes = []
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Set up Streamlit page
|
| 19 |
st.set_page_config(page_title="Quotation of the Day", page_icon="π", layout="centered")
|
| 20 |
st.title("π Quotation of the Day")
|
| 21 |
|
|
|
|
| 25 |
# AI-Generated Quote Image
|
| 26 |
st.subheader("πΌοΈ AI-Generated Quote Image")
|
| 27 |
if st.button("Generate Quote Image"):
|
| 28 |
+
# Call OpenAI's DALLΒ·E model to generate an image from the quote
|
| 29 |
+
response = openai.Image.create(
|
| 30 |
+
prompt=random_text, # Use the random quote as the prompt
|
| 31 |
+
n=1, # Generate 1 image
|
| 32 |
+
size="1024x1024" # Image size
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Extract the image URL from the response
|
| 36 |
+
image_url = response['data'][0]['url']
|
| 37 |
+
|
| 38 |
+
# Display the image
|
| 39 |
+
st.image(image_url, caption="AI-Generated Quote Image")
|
| 40 |
|
| 41 |
+
# AI-Generated Quote (Using GPT-3)
|
| 42 |
st.subheader("π‘ Get an AI-Generated Quote")
|
| 43 |
if st.button("Generate AI Quote"):
|
| 44 |
+
# Call OpenAI's GPT-3 model to generate a quote
|
| 45 |
+
response = openai.Completion.create(
|
| 46 |
+
engine="text-davinci-003", # Choose the GPT-3 model
|
| 47 |
+
prompt="A great quote is", # Starting text for the quote
|
| 48 |
+
max_tokens=50 # Limit the number of tokens (words/characters)
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Get the generated quote
|
| 52 |
+
ai_quote = response.choices[0].text.strip()
|
| 53 |
+
|
| 54 |
+
# Display the AI-generated quote
|
| 55 |
st.write(f"β¨ *{ai_quote}* β AI")
|
| 56 |
|
| 57 |
+
# User Authentication with Hugging Face (In this case, weβre not using it)
|
| 58 |
+
st.subheader("π Submit Your Own Quote")
|
| 59 |
if "username" not in st.session_state:
|
| 60 |
+
username = st.text_input("Enter your name (for quote submission)")
|
| 61 |
+
if st.button("Submit"):
|
| 62 |
st.session_state.username = username
|
| 63 |
st.success(f"β
Logged in as {username}")
|
| 64 |
|
|
|
|
| 66 |
if "username" in st.session_state:
|
| 67 |
st.subheader("π Submit Your Own Quote")
|
| 68 |
user_quote = st.text_area("Enter your quote")
|
| 69 |
+
|
| 70 |
+
if st.button("Submit Quote"):
|
|
|
|
| 71 |
if user_quote.strip():
|
| 72 |
+
st.session_state.user_quotes.append({'quote': user_quote, 'author': st.session_state.username})
|
|
|
|
| 73 |
st.success("β
Quote added successfully!")
|
|
|
|
| 74 |
else:
|
| 75 |
st.error("β Please enter a quote!")
|
| 76 |
|
| 77 |
+
# Display community quotes (user-submitted quotes)
|
| 78 |
st.subheader("π Community Quotes")
|
| 79 |
+
for i, q in enumerate(st.session_state.user_quotes):
|
| 80 |
col1, col2 = st.columns([4, 1])
|
| 81 |
with col1:
|
| 82 |
+
st.write(f"π¬ *{q['quote']}* β {q['author']}")
|
| 83 |
with col2:
|
| 84 |
+
if st.button(f"Upvote {i}", key=f"upvote_{i}"):
|
| 85 |
+
st.session_state.user_quotes[i]['upvotes'] = st.session_state.user_quotes.get('upvotes', 0) + 1
|
| 86 |
+
st.experimental_rerun()
|
|
|