Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,39 @@
|
|
1 |
import streamlit as st
|
2 |
import random
|
3 |
-
import
|
4 |
-
from
|
5 |
-
import
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
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 |
|
22 |
# Show a predefined quote
|
23 |
st.write(f"π£οΈ *{random_text}* β {random_author}")
|
24 |
|
25 |
-
# AI-Generated Quote
|
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 |
-
|
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
|
58 |
-
st.subheader("π
|
59 |
if "username" not in st.session_state:
|
60 |
-
username = st.text_input("Enter
|
61 |
-
if st.button("
|
62 |
st.session_state.username = username
|
63 |
st.success(f"β
Logged in as {username}")
|
64 |
|
@@ -66,21 +41,25 @@ if "username" not in st.session_state:
|
|
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 |
-
|
|
|
71 |
if user_quote.strip():
|
72 |
-
|
|
|
73 |
st.success("β
Quote added successfully!")
|
|
|
74 |
else:
|
75 |
st.error("β Please enter a quote!")
|
76 |
|
77 |
-
#
|
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 |
-
|
86 |
-
st.
|
|
|
|
1 |
import streamlit as st
|
2 |
import random
|
3 |
+
from datasets import load_dataset
|
4 |
+
from transformers import pipeline
|
5 |
+
from dataset import load_user_quotes, save_user_quote, upvote_quote
|
6 |
|
7 |
+
# Load predefined quotes dataset
|
8 |
+
dataset = load_dataset("Abirate/english_quotes")
|
9 |
+
random_quote = dataset['train'][random.randint(0, len(dataset['train'])-1)]
|
10 |
+
random_text = random_quote['quote']
|
11 |
+
random_author = random_quote['author']
|
|
|
12 |
|
13 |
# Load user-submitted quotes
|
14 |
if "user_quotes" not in st.session_state:
|
15 |
+
st.session_state.user_quotes = load_user_quotes()
|
16 |
+
|
17 |
+
# Load AI quote generator (GPT-2)
|
18 |
+
quote_generator = pipeline("text-generation", model="gpt2")
|
19 |
|
|
|
20 |
st.set_page_config(page_title="Quotation of the Day", page_icon="π", layout="centered")
|
21 |
st.title("π Quotation of the Day")
|
22 |
|
23 |
# Show a predefined quote
|
24 |
st.write(f"π£οΈ *{random_text}* β {random_author}")
|
25 |
|
26 |
+
# AI-Generated Quote
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
st.subheader("π‘ Get an AI-Generated Quote")
|
28 |
if st.button("Generate AI Quote"):
|
29 |
+
ai_quote = quote_generator("A great quote is", max_length=50, num_return_sequences=1)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
st.write(f"β¨ *{ai_quote}* β AI")
|
31 |
|
32 |
+
# User Authentication with Hugging Face
|
33 |
+
st.subheader("π Login with Hugging Face")
|
34 |
if "username" not in st.session_state:
|
35 |
+
username = st.text_input("Enter Hugging Face username (for quote submission)")
|
36 |
+
if st.button("Login"):
|
37 |
st.session_state.username = username
|
38 |
st.success(f"β
Logged in as {username}")
|
39 |
|
|
|
41 |
if "username" in st.session_state:
|
42 |
st.subheader("π Submit Your Own Quote")
|
43 |
user_quote = st.text_area("Enter your quote")
|
44 |
+
user_name = st.text_input("Your Name (Leave blank for Anonymous)")
|
45 |
+
|
46 |
+
if st.button("Submit"):
|
47 |
if user_quote.strip():
|
48 |
+
save_user_quote(user_quote, user_name if user_name else "Anonymous")
|
49 |
+
st.session_state.user_quotes = load_user_quotes() # Update session state
|
50 |
st.success("β
Quote added successfully!")
|
51 |
+
st.rerun() # Use st.rerun() instead of st.experimental_rerun()
|
52 |
else:
|
53 |
st.error("β Please enter a quote!")
|
54 |
|
55 |
+
# Community Quotes with Upvotes
|
56 |
st.subheader("π Community Quotes")
|
57 |
+
for i, q in enumerate(st.session_state.user_quotes): # Use session state for user_quotes
|
58 |
col1, col2 = st.columns([4, 1])
|
59 |
with col1:
|
60 |
+
st.write(f"π¬ *{q['quote']}* β {q['author']} | π {q['upvotes']} upvotes")
|
61 |
with col2:
|
62 |
+
if st.button(f"Upvote {i}", key=f"upvote_{i}"): # Indented correctly now
|
63 |
+
upvote_quote(i)
|
64 |
+
st.session_state.user_quotes = load_user_quotes() # Update session state
|
65 |
+
st.rerun() # Use st.rerun() here as well
|