File size: 2,647 Bytes
f187973
 
68bd574
 
 
f187973
68bd574
 
 
 
 
f187973
 
072edf1
68bd574
 
 
 
f187973
 
 
 
 
 
 
68bd574
f187973
 
68bd574
f187973
 
68bd574
 
f187973
68bd574
 
f187973
 
 
 
 
 
 
68bd574
 
 
8ee5ed6
68bd574
 
8ee5ed6
68bd574
8ee5ed6
 
 
68bd574
f187973
68bd574
f187973
 
68bd574
f187973
68bd574
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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