Spaces:
Sleeping
Sleeping
import streamlit as st | |
import random | |
from datasets import load_dataset | |
from transformers import pipeline | |
from dataset import load_user_quotes, save_user_quote, upvote_quote | |
# Load predefined quotes dataset | |
dataset = load_dataset("Abirate/english_quotes") | |
random_quote = dataset['train'][random.randint(0, len(dataset['train'])-1)] | |
random_text = random_quote['quote'] | |
random_author = random_quote['author'] | |
# Load user-submitted quotes | |
if "user_quotes" not in st.session_state: | |
st.session_state.user_quotes = load_user_quotes() | |
# Load AI quote generator (GPT-2) | |
quote_generator = pipeline("text-generation", model="gpt2") | |
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 | |
st.subheader("π‘ Get an AI-Generated Quote") | |
if st.button("Generate AI Quote"): | |
ai_quote = quote_generator("A great quote is", max_length=50, num_return_sequences=1)[0]["generated_text"] | |
st.write(f"β¨ *{ai_quote}* β AI") | |
# User Authentication with Hugging Face | |
st.subheader("π Login with Hugging Face") | |
if "username" not in st.session_state: | |
username = st.text_input("Enter Hugging Face username (for quote submission)") | |
if st.button("Login"): | |
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") | |
user_name = st.text_input("Your Name (Leave blank for Anonymous)") | |
if st.button("Submit"): | |
if user_quote.strip(): | |
save_user_quote(user_quote, user_name if user_name else "Anonymous") | |
st.session_state.user_quotes = load_user_quotes() # Update session state | |
st.success("β Quote added successfully!") | |
st.rerun() # Use st.rerun() instead of st.experimental_rerun() | |
else: | |
st.error("β Please enter a quote!") | |
# Community Quotes with Upvotes | |
st.subheader("π Community Quotes") | |
for i, q in enumerate(st.session_state.user_quotes): # Use session state for user_quotes | |
col1, col2 = st.columns([4, 1]) | |
with col1: | |
st.write(f"π¬ *{q['quote']}* β {q['author']} | π {q['upvotes']} upvotes") | |
with col2: | |
if st.button(f"Upvote {i}", key=f"upvote_{i}"): # Indented correctly now | |
upvote_quote(i) | |
st.session_state.user_quotes = load_user_quotes() # Update session state | |
st.rerun() # Use st.rerun() here as well | |