NayabShakeel commited on
Commit
68bd574
Β·
verified Β·
1 Parent(s): d9d4282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -52
app.py CHANGED
@@ -1,64 +1,39 @@
1
  import streamlit as st
2
  import random
3
- import openai
4
- from PIL import Image
5
- import requests
6
 
7
- # Set your OpenAI API key
8
- openai.api_key = "OPENAI_API_KEY" # Replace with your OpenAI API key
9
-
10
- # Load predefined quotes dataset (Here, using a static example for now)
11
- random_text = "The only way to do great work is to love what you do."
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 Image
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
- # Call OpenAI's GPT-3 model to generate a quote
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 (In this case, we’re not using it)
58
- st.subheader("πŸ”‘ Submit Your Own Quote")
59
  if "username" not in st.session_state:
60
- username = st.text_input("Enter your name (for quote submission)")
61
- if st.button("Submit"):
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
- if st.button("Submit Quote"):
 
71
  if user_quote.strip():
72
- st.session_state.user_quotes.append({'quote': user_quote, 'author': st.session_state.username})
 
73
  st.success("βœ… Quote added successfully!")
 
74
  else:
75
  st.error("❌ Please enter a quote!")
76
 
77
- # Display community quotes (user-submitted quotes)
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
- st.session_state.user_quotes[i]['upvotes'] = st.session_state.user_quotes.get('upvotes', 0) + 1
86
- st.experimental_rerun()
 
 
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