Commit ·
b28b5d3
1
Parent(s): 0af0afb
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load the fine-tuned model and tokenizer
|
| 6 |
+
model_name = "."
|
| 7 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 8 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Function to generate text based on input
|
| 11 |
+
def generate_text(input_text):
|
| 12 |
+
# Tokenize and generate text with sampling and different decoding parameters
|
| 13 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512)
|
| 14 |
+
generated_text = model.generate(
|
| 15 |
+
input_ids,
|
| 16 |
+
max_length=200,
|
| 17 |
+
num_beams=5,
|
| 18 |
+
temperature=0.9, # Adjust the temperature for more randomness
|
| 19 |
+
no_repeat_ngram_size=2,
|
| 20 |
+
top_k=50,
|
| 21 |
+
top_p=0.95,
|
| 22 |
+
early_stopping=True,
|
| 23 |
+
do_sample=True,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Decode and return the generated text
|
| 27 |
+
decoded_text = tokenizer.decode(generated_text[0], skip_special_tokens=True)
|
| 28 |
+
return decoded_text
|
| 29 |
+
|
| 30 |
+
# Streamlit app
|
| 31 |
+
def main():
|
| 32 |
+
|
| 33 |
+
# Apply custom styling for the title
|
| 34 |
+
st.markdown("<h3 style='text-align: center; color: #333;'>Medical Summary - Text Generation</h3>", unsafe_allow_html=True)
|
| 35 |
+
|
| 36 |
+
# Textbox for user input
|
| 37 |
+
user_input = st.text_area("Enter Text:", "")
|
| 38 |
+
|
| 39 |
+
# Button to trigger text generation
|
| 40 |
+
if st.button("Compute"):
|
| 41 |
+
if user_input:
|
| 42 |
+
# Call the generate_text function with user input
|
| 43 |
+
result = generate_text(user_input)
|
| 44 |
+
|
| 45 |
+
# Display the generated text in a box with word wrap
|
| 46 |
+
#st.markdown(f"**Generated Text:**\n\n```\n{result}\n```", unsafe_allow_html=True)
|
| 47 |
+
#st.text(result)
|
| 48 |
+
# Display the generated text in a div with word wrap and auto-increasing height
|
| 49 |
+
#st.markdown(f"<div style='white-space: pre-wrap; overflow-y: auto;'>**Generated Text:**\n\n```\n{result}\n```</div>", unsafe_allow_html=True)
|
| 50 |
+
# Display the generated text in a div with word wrap and auto-increasing width and height
|
| 51 |
+
#st.markdown(f"<div style='white-space: pre-wrap; width: 100%; overflow: auto;'>**Generated Text:**\n\n```\n{result}\n```</div>", unsafe_allow_html=True)
|
| 52 |
+
# Display the generated text in a text area with word wrap
|
| 53 |
+
st.text_area("Generated Text:", result, key="generated_text")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Run the Streamlit app
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
main()
|