Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the Microsoft BioGPT text generation pipeline
|
| 5 |
+
generator = pipeline("text-generation", model="microsoft/BioGPT")
|
| 6 |
+
|
| 7 |
+
def generate_medical_text(prompt, temperature, top_p, max_length):
|
| 8 |
+
# Clamp temperature to at least 0.01 to avoid division by zero errors
|
| 9 |
+
temperature = max(0.01, float(temperature))
|
| 10 |
+
|
| 11 |
+
# Generate text using the pipeline
|
| 12 |
+
results = generator(
|
| 13 |
+
prompt,
|
| 14 |
+
max_length=int(max_length),
|
| 15 |
+
temperature=temperature,
|
| 16 |
+
top_p=float(top_p),
|
| 17 |
+
do_sample=True,
|
| 18 |
+
num_return_sequences=1,
|
| 19 |
+
truncation=True,
|
| 20 |
+
pad_token_id=generator.tokenizer.eos_token_id # Prevents padding warnings
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
return results[0]["generated_text"]
|
| 24 |
+
|
| 25 |
+
# Define interface text
|
| 26 |
+
title = "Medical Text Generator"
|
| 27 |
+
description = (
|
| 28 |
+
"Designed for experimenting with medical and health-related text — "
|
| 29 |
+
"clinical notes, symptom descriptions, patient scenarios, and health explanations. "
|
| 30 |
+
"Powered by Microsoft's `BioGPT` model, which was trained on millions of biomedical research articles."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Define preset examples (Format: [prompt, temperature, top_p, max_length])
|
| 34 |
+
examples = [["The patient presented with symptoms of", 0.5, 0.9, 120],["Common side effects of this medication include", 0.5, 0.9, 120],["The doctor examined the test results and concluded", 0.5, 0.9, 120],["A healthy diet for someone with diabetes should", 0.5, 0.9, 120],["The difference between a virus and a bacteria is", 0.5, 0.9, 120]
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
# Build the Gradio interface
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate_medical_text,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(
|
| 42 |
+
lines=3,
|
| 43 |
+
label="Prompt",
|
| 44 |
+
placeholder="Enter a medical prompt here..."
|
| 45 |
+
),
|
| 46 |
+
gr.Slider(
|
| 47 |
+
minimum=0.1, maximum=2.0, value=0.5, step=0.1,
|
| 48 |
+
label="Temperature",
|
| 49 |
+
info="Controls how creative/wild the writing is"
|
| 50 |
+
),
|
| 51 |
+
gr.Slider(
|
| 52 |
+
minimum=0.1, maximum=1.0, value=0.9, step=0.05,
|
| 53 |
+
label="Top-p",
|
| 54 |
+
info="Controls word diversity"
|
| 55 |
+
),
|
| 56 |
+
gr.Slider(
|
| 57 |
+
minimum=20, maximum=200, value=120, step=1,
|
| 58 |
+
label="Max Length",
|
| 59 |
+
info="Controls how much text it generates"
|
| 60 |
+
)
|
| 61 |
+
],
|
| 62 |
+
outputs=gr.Textbox(label="Generated Text", lines=8),
|
| 63 |
+
title=title,
|
| 64 |
+
description=description,
|
| 65 |
+
examples=examples
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch()
|