Spaces:
Build error
Build error
Commit ·
5054d57
1
Parent(s): c6ba260
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,84 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import urllib
|
| 3 |
+
import re
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
+
# Set the page config
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
layout="centered", # or "centered"
|
| 10 |
+
initial_sidebar_state="auto", # or "expanded" or "collapsed"
|
| 11 |
+
page_title="WordGPT", # or any other title
|
| 12 |
+
page_icon="📝" # or any other emoji or image
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
# Give a large title to the page
|
| 16 |
+
st.title("WordGPT")
|
| 17 |
|
| 18 |
+
# read the csv file
|
| 19 |
+
df = pd.read_csv("ielts.csv")
|
| 20 |
|
| 21 |
+
# create two columns
|
| 22 |
+
col1, col2 = st.columns(2)
|
| 23 |
+
|
| 24 |
+
# get all the values from the first column of the dataframe as a list
|
| 25 |
+
values = df.iloc[:,0].tolist()
|
| 26 |
+
|
| 27 |
+
# join the values with newline characters as a string
|
| 28 |
+
text = "\n".join(values)
|
| 29 |
+
|
| 30 |
+
placeholder = col1.empty()
|
| 31 |
+
|
| 32 |
+
# display the value in the textarea of the first column
|
| 33 |
+
words = placeholder.text_area("Value from ielts.csv", text, height=200)
|
| 34 |
+
|
| 35 |
+
# Create a button in col1 named "Shuffle the words"
|
| 36 |
+
shuffle_button = col1.button("Shuffle the words")
|
| 37 |
+
|
| 38 |
+
# Shuffle the words in the text area and display them in col2
|
| 39 |
+
if shuffle_button:
|
| 40 |
+
words = words.split("\n")
|
| 41 |
+
random.shuffle(words)
|
| 42 |
+
words = "\n".join(words)
|
| 43 |
+
placeholder.empty()
|
| 44 |
+
words = placeholder.text_area("Shuffle the words", words, height=200)
|
| 45 |
+
|
| 46 |
+
# Check if the user has entered any words
|
| 47 |
+
if words:
|
| 48 |
+
# Split the words by any common symbol or line break and strip any whitespace
|
| 49 |
+
words = [word.strip() for word in re.split("[,.;:!?]|\\n", words)]
|
| 50 |
+
|
| 51 |
+
# Select only the first 12 words of the list
|
| 52 |
+
words = " ".join(re.findall(r"\w+", " ".join(words))[:12])
|
| 53 |
+
|
| 54 |
+
# Create a Bing query in the desired format
|
| 55 |
+
query = f"write a short story with these words involved: {words}"
|
| 56 |
+
query_origin = query
|
| 57 |
+
|
| 58 |
+
# Replace all spaces with "+" symbols
|
| 59 |
+
# query = query.replace(" ", "+")
|
| 60 |
+
|
| 61 |
+
# Encode the query for the URL
|
| 62 |
+
query = urllib.parse.quote(query)
|
| 63 |
+
|
| 64 |
+
# Create a Bing URL with the query
|
| 65 |
+
url = f"https://www.bing.com/search?q={query}"
|
| 66 |
+
|
| 67 |
+
# Put the rest of the elements in the right column
|
| 68 |
+
col2.write(f"**Your Bing query is:** {query_origin}")
|
| 69 |
+
col2.markdown(f'<a href="{url}" target="_blank"><button>Let Bing AI create a story</button></a>', unsafe_allow_html=True)
|
| 70 |
+
|
| 71 |
+
# Create a form in the right column with the clear_on_submit parameter set to True
|
| 72 |
+
with col2.form("story_form", clear_on_submit=True):
|
| 73 |
+
# Put a textarea in the form
|
| 74 |
+
story = st.text_area("Enter your own story or edit the Bing AI story", value="", height=200)
|
| 75 |
+
|
| 76 |
+
# Put a submit button in the form
|
| 77 |
+
submitted = st.form_submit_button("Submit")
|
| 78 |
+
|
| 79 |
+
# Check if the user has submitted the form
|
| 80 |
+
if submitted:
|
| 81 |
+
# Open the file in append mode
|
| 82 |
+
with open("my_story.txt", "a") as f:
|
| 83 |
+
# Write the story to the file
|
| 84 |
+
f.write(story + "\n")
|