NayabShakeel commited on
Commit
f187973
Β·
verified Β·
1 Parent(s): 997e5f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
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()