bash98 commited on
Commit
7ccd5f2
·
verified ·
1 Parent(s): c3a3872

Create app.py

Browse files

app for my health summarizer app.

Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ MODEL_NAME = "google/flan-t5-small"
6
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME).to(DEVICE)
10
+
11
+ DISCLAIMER = (
12
+ "Disclaimer: This tool provides general health information only. "
13
+ "It is not medical advice and does not replace consultation with a qualified healthcare professional. "
14
+ "For diagnosis, treatment, or emergencies, please see a doctor or visit a health facility immediately."
15
+ )
16
+
17
+ def generate_summary(text: str, max_tokens: int = 200) -> str:
18
+ prompt = (
19
+ "Summarize the following health information in simple language for a non-medical person:\n\n"
20
+ f"{text}\n\nSummary:"
21
+ )
22
+ inputs = tokenizer(
23
+ prompt,
24
+ return_tensors="pt",
25
+ truncation=True,
26
+ max_length=1024
27
+ ).to(DEVICE)
28
+
29
+ with torch.no_grad():
30
+ outputs = model.generate(
31
+ **inputs,
32
+ max_new_tokens=max_tokens,
33
+ num_beams=4,
34
+ early_stopping=True,
35
+ )
36
+
37
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
+ return summary.strip()
39
+
40
+ def summarizer_interface(long_text: str) -> str:
41
+ if not long_text or len(long_text.strip()) == 0:
42
+ return "Please paste a health-related text to summarize."
43
+ summary = generate_summary(long_text)
44
+ return f"{summary}\n\n{DISCLAIMER}"
45
+
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("# 🩺 Health Document Summarizer (Educational Use Only)")
48
+ gr.Markdown(
49
+ "Paste any health-related text. "
50
+ "The model will summarize it in simpler language. "
51
+ "Do not use this tool for emergencies or as a replacement for a doctor."
52
+ )
53
+
54
+ input_box = gr.Textbox(
55
+ label="Input health text",
56
+ placeholder="Paste health information here...",
57
+ lines=10
58
+ )
59
+ output_box = gr.Textbox(
60
+ label="Summarized explanation",
61
+ lines=10
62
+ )
63
+ summarize_button = gr.Button("Summarize")
64
+
65
+ summarize_button.click(
66
+ summarizer_interface,
67
+ inputs=input_box,
68
+ outputs=output_box
69
+ )
70
+
71
+ if __name__ == "__main__":
72
+ demo.launch()