NayabShakeel commited on
Commit
1bdcde8
Β·
verified Β·
1 Parent(s): 92febcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -39
app.py CHANGED
@@ -1,24 +1,21 @@
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
  if "user_quotes" not in st.session_state:
17
- st.session_state.user_quotes = load_user_quotes()
18
-
19
- # Load AI quote generator (GPT-2)
20
- quote_generator = pipeline("text-generation", model="gpt2")
21
 
 
22
  st.set_page_config(page_title="Quotation of the Day", page_icon="πŸ“", layout="centered")
23
  st.title("πŸ“œ Quotation of the Day")
24
 
@@ -28,26 +25,40 @@ st.write(f"πŸ—£οΈ *{random_text}* – {random_author}")
28
  # AI-Generated Quote Image
29
  st.subheader("πŸ–ΌοΈ AI-Generated Quote Image")
30
  if st.button("Generate Quote Image"):
31
- response = requests.get(f"https://api-inference.huggingface.co/models/dalle-mini",
32
- headers={"Authorization": "Bearer YOUR_HF_API_KEY"},
33
- json={"inputs": random_text})
34
- if response.status_code == 200:
35
- image = Image.open(response.content)
36
- st.image(image, caption="AI-Generated Quote Image")
37
- else:
38
- st.error("Failed to generate image.")
 
 
 
 
39
 
40
- # AI-Generated Quote
41
  st.subheader("πŸ’‘ Get an AI-Generated Quote")
42
  if st.button("Generate AI Quote"):
43
- ai_quote = quote_generator("A great quote is", max_length=50, num_return_sequences=1)[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
44
  st.write(f"✨ *{ai_quote}* – AI")
45
 
46
- # User Authentication with Hugging Face
47
- st.subheader("πŸ”‘ Login with Hugging Face")
48
  if "username" not in st.session_state:
49
- username = st.text_input("Enter Hugging Face username (for quote submission)")
50
- if st.button("Login"):
51
  st.session_state.username = username
52
  st.success(f"βœ… Logged in as {username}")
53
 
@@ -55,25 +66,21 @@ if "username" not in st.session_state:
55
  if "username" in st.session_state:
56
  st.subheader("πŸ“ Submit Your Own Quote")
57
  user_quote = st.text_area("Enter your quote")
58
- user_name = st.text_input("Your Name (Leave blank for Anonymous)")
59
-
60
- if st.button("Submit"):
61
  if user_quote.strip():
62
- save_user_quote(user_quote, user_name if user_name else "Anonymous")
63
- st.session_state.user_quotes = load_user_quotes() # Update session state
64
  st.success("βœ… Quote added successfully!")
65
- st.rerun() # Use st.rerun() instead of st.experimental_rerun()
66
  else:
67
  st.error("❌ Please enter a quote!")
68
 
69
- # Community Quotes with Upvotes
70
  st.subheader("πŸ“– Community Quotes")
71
- for i, q in enumerate(st.session_state.user_quotes): # Use session state for user_quotes
72
  col1, col2 = st.columns([4, 1])
73
  with col1:
74
- st.write(f"πŸ’¬ *{q['quote']}* – {q['author']} | πŸ‘ {q['upvotes']} upvotes")
75
  with col2:
76
- if st.button(f"Upvote {i}", key=f"upvote_{i}"): # Indented correctly now
77
- upvote_quote(i)
78
- st.session_state.user_quotes = load_user_quotes() # Update session state
79
- st.rerun() # Use st.rerun() here as well
 
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 = "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" # 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
 
 
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
  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()