Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
# Load models and tokenizers
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_healthscribe_model():
|
| 7 |
+
model_name = "har1/HealthScribe-Clinical_Note_Generator"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
+
return model, tokenizer
|
| 11 |
+
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_translation_model(model_name):
|
| 14 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 15 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 16 |
+
return model, tokenizer
|
| 17 |
+
|
| 18 |
+
# Initialize models
|
| 19 |
+
healthscribe_model, healthscribe_tokenizer = load_healthscribe_model()
|
| 20 |
+
|
| 21 |
+
# Language selection options
|
| 22 |
+
language_options = {
|
| 23 |
+
"English to French": ("en", "fr"),
|
| 24 |
+
"French to English": ("fr", "en"),
|
| 25 |
+
"English to Spanish": ("en", "es"),
|
| 26 |
+
"Spanish to English": ("es", "en"),
|
| 27 |
+
"English to German": ("en", "de"),
|
| 28 |
+
"German to English": ("de", "en"),
|
| 29 |
+
"English to Italian": ("en", "it"),
|
| 30 |
+
"Italian to English": ("it", "en"),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Streamlit UI setup
|
| 34 |
+
st.title("Multifunctional Text Processing App")
|
| 35 |
+
st.write("This app can generate clinical notes or translate text between languages.")
|
| 36 |
+
|
| 37 |
+
# Choose task
|
| 38 |
+
task = st.selectbox("Select a task:", ["Generate Clinical Note", "Translate Text"])
|
| 39 |
+
|
| 40 |
+
if task == "Generate Clinical Note":
|
| 41 |
+
st.subheader("Clinical Note Generator")
|
| 42 |
+
input_text = st.text_area("Enter patient information or medical notes:", height=200)
|
| 43 |
+
|
| 44 |
+
if st.button("Generate Clinical Note"):
|
| 45 |
+
if input_text.strip():
|
| 46 |
+
# Tokenize and generate
|
| 47 |
+
inputs = healthscribe_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 48 |
+
outputs = healthscribe_model.generate(inputs["input_ids"], max_length=512, num_beams=5, early_stopping=True)
|
| 49 |
+
generated_note = healthscribe_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
# Display the result
|
| 52 |
+
st.subheader("Generated Clinical Note")
|
| 53 |
+
st.write(generated_note)
|
| 54 |
+
else:
|
| 55 |
+
st.warning("Please enter some text to generate a clinical note.")
|
| 56 |
+
|
| 57 |
+
elif task == "Translate Text":
|
| 58 |
+
st.subheader("Translation Tool")
|
| 59 |
+
language_pair = st.selectbox("Select language pair", list(language_options.keys()))
|
| 60 |
+
src_lang, tgt_lang = language_options[language_pair]
|
| 61 |
+
|
| 62 |
+
# Load the corresponding translation model and tokenizer
|
| 63 |
+
model_name = f"Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}"
|
| 64 |
+
translation_model, translation_tokenizer = load_translation_model(model_name)
|
| 65 |
+
|
| 66 |
+
# Input text to translate
|
| 67 |
+
text = st.text_area("Enter text to translate:")
|
| 68 |
+
|
| 69 |
+
if st.button("Translate"):
|
| 70 |
+
if text.strip():
|
| 71 |
+
# Prepare the input for the model
|
| 72 |
+
inputs = translation_tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 73 |
+
|
| 74 |
+
# Generate translation
|
| 75 |
+
translation = translation_model.generate(**inputs)
|
| 76 |
+
|
| 77 |
+
# Decode the output
|
| 78 |
+
translated_text = translation_tokenizer.decode(translation[0], skip_special_tokens=True)
|
| 79 |
+
|
| 80 |
+
# Display translation
|
| 81 |
+
st.write("**Original Text**:", text)
|
| 82 |
+
st.write("**Translated Text**:", translated_text)
|
| 83 |
+
else:
|
| 84 |
+
st.warning("Please enter some text to translate.")
|