Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,13 @@
|
|
2 |
#Create a torch demonstration and use data created with this program below for input and output. Design a torch demo that uses simple convolutions to explain correlation between one word and another and add to IO patterns of this program and use requests and nlp including nltk to remix a demonstration app that uses the text files as input.
|
3 |
#Have this allow users to build their library of prompts and add it to their user file defined by email address named text file.
|
4 |
#Add links to sites with reference documentation. Use this as teaching lesson in python streamlit UI and code yet remove all comments and just have variable names be super descriptive
|
5 |
-
|
6 |
import streamlit as st
|
7 |
import torch
|
8 |
import torch.nn as nn
|
9 |
import nltk
|
10 |
from nltk.corpus import stopwords
|
11 |
-
import
|
12 |
-
import
|
13 |
|
14 |
# Ensure NLTK resources are downloaded
|
15 |
nltk.download('punkt')
|
@@ -22,26 +21,37 @@ def text_convolution(input_text, kernel_size=3):
|
|
22 |
tensor_input = torch.tensor([hash(word) for word in words], dtype=torch.float)
|
23 |
conv_layer = nn.Conv1d(1, 1, kernel_size, stride=1)
|
24 |
tensor_input = tensor_input.view(1, 1, -1)
|
25 |
-
|
|
|
26 |
|
27 |
# Streamlit UI
|
28 |
def main():
|
29 |
st.title("Text Convolution Demonstration")
|
30 |
-
st.write("Upload a text file and see
|
31 |
|
32 |
-
uploaded_file = st.file_uploader("Choose a text file", type=["txt"])
|
33 |
user_email = st.text_input("Enter your email to save your prompts:")
|
|
|
34 |
if uploaded_file is not None and user_email:
|
35 |
text_data = uploaded_file.read().decode("utf-8")
|
36 |
-
conv_result = text_convolution(text_data)
|
37 |
st.write("Convolution result:", conv_result)
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# Saving user prompts
|
40 |
-
|
41 |
-
with open(
|
42 |
file.write(text_data + "\n")
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
main()
|
|
|
2 |
#Create a torch demonstration and use data created with this program below for input and output. Design a torch demo that uses simple convolutions to explain correlation between one word and another and add to IO patterns of this program and use requests and nlp including nltk to remix a demonstration app that uses the text files as input.
|
3 |
#Have this allow users to build their library of prompts and add it to their user file defined by email address named text file.
|
4 |
#Add links to sites with reference documentation. Use this as teaching lesson in python streamlit UI and code yet remove all comments and just have variable names be super descriptive
|
|
|
5 |
import streamlit as st
|
6 |
import torch
|
7 |
import torch.nn as nn
|
8 |
import nltk
|
9 |
from nltk.corpus import stopwords
|
10 |
+
import pandas as pd
|
11 |
+
import base64
|
12 |
|
13 |
# Ensure NLTK resources are downloaded
|
14 |
nltk.download('punkt')
|
|
|
21 |
tensor_input = torch.tensor([hash(word) for word in words], dtype=torch.float)
|
22 |
conv_layer = nn.Conv1d(1, 1, kernel_size, stride=1)
|
23 |
tensor_input = tensor_input.view(1, 1, -1)
|
24 |
+
output = conv_layer(tensor_input)
|
25 |
+
return output, words
|
26 |
|
27 |
# Streamlit UI
|
28 |
def main():
|
29 |
st.title("Text Convolution Demonstration")
|
30 |
+
st.write("This app demonstrates how text convolution works. Upload a text file and see the convolution result along with a distribution plot of word tokens.")
|
31 |
|
32 |
+
uploaded_file = st.file_uploader("Choose a text file (TXT only)", type=["txt"])
|
33 |
user_email = st.text_input("Enter your email to save your prompts:")
|
34 |
+
|
35 |
if uploaded_file is not None and user_email:
|
36 |
text_data = uploaded_file.read().decode("utf-8")
|
37 |
+
conv_result, words = text_convolution(text_data)
|
38 |
st.write("Convolution result:", conv_result)
|
39 |
|
40 |
+
# Visualization
|
41 |
+
word_counts = pd.Series(words).value_counts()
|
42 |
+
st.bar_chart(word_counts.head(10))
|
43 |
+
|
44 |
# Saving user prompts
|
45 |
+
user_file_name = f"{user_email}_prompts.txt"
|
46 |
+
with open(user_file_name, "a") as file:
|
47 |
file.write(text_data + "\n")
|
48 |
+
st.success(f"Your prompts have been added to {user_file_name}")
|
49 |
|
50 |
+
# Download link for the file
|
51 |
+
with open(user_file_name, "rb") as f:
|
52 |
+
b64 = base64.b64encode(f.read()).decode()
|
53 |
+
href = f'<a href="data:file/txt;base64,{b64}" download="{user_file_name}">Download {user_file_name}</a>'
|
54 |
+
st.markdown(href, unsafe_allow_html=True)
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
main()
|