|
|
|
|
|
|
|
|
|
|
|
import streamlit as st |
|
import torch |
|
import torch.nn as nn |
|
import nltk |
|
from nltk.corpus import stopwords |
|
import requests |
|
import os |
|
|
|
|
|
nltk.download('punkt') |
|
nltk.download('stopwords') |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|