awacke1 commited on
Commit
48ad8c6
·
verified ·
1 Parent(s): a62e5bf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #TorchNeuralNetworkNLTKRequests
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 requests
12
+ import os
13
+
14
+ # Ensure NLTK resources are downloaded
15
+ nltk.download('punkt')
16
+ nltk.download('stopwords')
17
+
18
+ # Function to perform convolution on text data
19
+ def text_convolution(input_text, kernel_size=3):
20
+ words = nltk.word_tokenize(input_text)
21
+ words = [word for word in words if word not in stopwords.words('english')]
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
+ return conv_layer(tensor_input)
26
+
27
+ # Streamlit UI
28
+ def main():
29
+ st.title("Text Convolution Demonstration")
30
+ st.write("Upload a text file and see how text convolution works.")
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
+ user_file_path = f"{user_email}_prompts.txt"
41
+ with open(user_file_path, "a") as file:
42
+ file.write(text_data + "\n")
43
+
44
+ st.write(f"Your prompts have been added to {user_file_path}")
45
+
46
+ if __name__ == "__main__":
47
+ main()
48
+
49
+
50
+ #Create requirements.txt import streamlit as st
51
+ #import torch
52
+ #import torch.nn as nn
53
+ #import nltk
54
+ #from nltk.corpus import stopwords
55
+ #import requests
56
+ #import os
57
+
58
+ #streamlit
59
+ #torch
60
+ #nltk
61
+ #requests
62
+