File size: 2,339 Bytes
48ad8c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#TorchNeuralNetworkNLTKRequests
#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.  
#Have this allow users to build their library of prompts and add it to their user file defined by email address named text file.  
#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

import streamlit as st
import torch
import torch.nn as nn
import nltk
from nltk.corpus import stopwords
import requests
import os

# Ensure NLTK resources are downloaded
nltk.download('punkt')
nltk.download('stopwords')

# Function to perform convolution on text data
def text_convolution(input_text, kernel_size=3):
    words = nltk.word_tokenize(input_text)
    words = [word for word in words if word not in stopwords.words('english')]
    tensor_input = torch.tensor([hash(word) for word in words], dtype=torch.float)
    conv_layer = nn.Conv1d(1, 1, kernel_size, stride=1)
    tensor_input = tensor_input.view(1, 1, -1)
    return conv_layer(tensor_input)

# Streamlit UI
def main():
    st.title("Text Convolution Demonstration")
    st.write("Upload a text file and see how text convolution works.")

    uploaded_file = st.file_uploader("Choose a text file", type=["txt"])
    user_email = st.text_input("Enter your email to save your prompts:")
    if uploaded_file is not None and user_email:
        text_data = uploaded_file.read().decode("utf-8")
        conv_result = text_convolution(text_data)
        st.write("Convolution result:", conv_result)

        # Saving user prompts
        user_file_path = f"{user_email}_prompts.txt"
        with open(user_file_path, "a") as file:
            file.write(text_data + "\n")

        st.write(f"Your prompts have been added to {user_file_path}")

if __name__ == "__main__":
    main()


#Create requirements.txt import streamlit as st
#import torch
#import torch.nn as nn
#import nltk
#from nltk.corpus import stopwords
#import requests
#import os

#streamlit
#torch
#nltk
#requests