Spaces:
Sleeping
Sleeping
start
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import nltk
|
| 4 |
+
|
| 5 |
+
# Download NLTK data
|
| 6 |
+
nltk.download('punkt')
|
| 7 |
+
|
| 8 |
+
# Load the tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("fabiochiu/t5-base-tag-generation")
|
| 11 |
+
|
| 12 |
+
# Streamlit app title
|
| 13 |
+
st.title("Text to Tags Generation")
|
| 14 |
+
|
| 15 |
+
# Text area for user input
|
| 16 |
+
text = st.text_area("Enter the text for tag extraction:", height=200)
|
| 17 |
+
|
| 18 |
+
# Button to generate tags
|
| 19 |
+
if st.button("Generate Tags"):
|
| 20 |
+
if text:
|
| 21 |
+
try:
|
| 22 |
+
# Tokenize and encode the input text
|
| 23 |
+
inputs = tokenizer([text], max_length=512, truncation=True, return_tensors="pt")
|
| 24 |
+
|
| 25 |
+
# Generate tags
|
| 26 |
+
output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=64)
|
| 27 |
+
|
| 28 |
+
# Decode the output
|
| 29 |
+
decoded_output = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 30 |
+
|
| 31 |
+
# Extract unique tags
|
| 32 |
+
tags = list(set(decoded_output.strip().split(", ")))
|
| 33 |
+
|
| 34 |
+
# Display the tags
|
| 35 |
+
st.write("**Generated Tags:**")
|
| 36 |
+
st.write(tags)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"An error occurred: {e}")
|
| 39 |
+
else:
|
| 40 |
+
st.warning("Please enter some text to generate tags.")
|
| 41 |
+
|
| 42 |
+
# To run this app, save this code to a file (e.g., `app.py`) and run `streamlit run app.py` in your terminal.
|