Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
|
9 |
+
# Load predefined quotes dataset
|
10 |
+
dataset = load_dataset("Abirate/english_quotes")
|
11 |
+
random_quote = dataset['train'][random.randint(0, len(dataset['train'])-1)]
|
12 |
+
random_text = random_quote['quote']
|
13 |
+
random_author = random_quote['author']
|
14 |
+
|
15 |
+
# Load user-submitted quotes
|
16 |
+
user_quotes = load_user_quotes()
|
17 |
+
|
18 |
+
# Load AI quote generator (GPT-2)
|
19 |
+
quote_generator = pipeline("text-generation", model="gpt2")
|
20 |
+
|
21 |
+
st.set_page_config(page_title="Quotation of the Day", page_icon="π", layout="centered")
|
22 |
+
st.title("π Quotation of the Day")
|
23 |
+
|
24 |
+
# Show a predefined quote
|
25 |
+
st.write(f"π£οΈ *{random_text}* β {random_author}")
|
26 |
+
|
27 |
+
# AI-Generated Quote Image
|
28 |
+
st.subheader("πΌοΈ AI-Generated Quote Image")
|
29 |
+
if st.button("Generate Quote Image"):
|
30 |
+
response = requests.get(f"https://api-inference.huggingface.co/models/dalle-mini",
|
31 |
+
headers={"Authorization": "Bearer YOUR_HF_API_KEY"},
|
32 |
+
json={"inputs": random_text})
|
33 |
+
if response.status_code == 200:
|
34 |
+
image = Image.open(response.content)
|
35 |
+
st.image(image, caption="AI-Generated Quote Image")
|
36 |
+
else:
|
37 |
+
st.error("Failed to generate image.")
|
38 |
+
|
39 |
+
# AI-Generated Quote
|
40 |
+
st.subheader("π‘ Get an AI-Generated Quote")
|
41 |
+
if st.button("Generate AI Quote"):
|
42 |
+
ai_quote = quote_generator("A great quote is", max_length=50, num_return_sequences=1)[0]["generated_text"]
|
43 |
+
st.write(f"β¨ *{ai_quote}* β AI")
|
44 |
+
|
45 |
+
# User Authentication with Hugging Face
|
46 |
+
st.subheader("π Login with Hugging Face")
|
47 |
+
if "username" not in st.session_state:
|
48 |
+
username = st.text_input("Enter Hugging Face username (for quote submission)")
|
49 |
+
if st.button("Login"):
|
50 |
+
st.session_state.username = username
|
51 |
+
st.success(f"β
Logged in as {username}")
|
52 |
+
|
53 |
+
# Submit Your Own Quote
|
54 |
+
if "username" in st.session_state:
|
55 |
+
st.subheader("π Submit Your Own Quote")
|
56 |
+
user_quote = st.text_area("Enter your quote")
|
57 |
+
user_name = st.text_input("Your Name (Leave blank for Anonymous)")
|
58 |
+
|
59 |
+
if st.button("Submit"):
|
60 |
+
if user_quote.strip():
|
61 |
+
save_user_quote(user_quote, user_name if user_name else "Anonymous")
|
62 |
+
st.success("β
Quote added successfully!")
|
63 |
+
else:
|
64 |
+
st.error("β Please enter a quote!")
|
65 |
+
|
66 |
+
# Community Quotes with Upvotes
|
67 |
+
st.subheader("π Community Quotes")
|
68 |
+
for i, q in enumerate(user_quotes):
|
69 |
+
col1, col2 = st.columns([4, 1])
|
70 |
+
with col1:
|
71 |
+
st.write(f"π¬ *{q['quote']}* β {q['author']} | π {q['upvotes']} upvotes")
|
72 |
+
with col2:
|
73 |
+
if st.button(f"Upvote {i}", key=f"upvote_{i}"):
|
74 |
+
upvote_quote(i)
|
75 |
+
st.experimental_rerun()
|