first commit
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
import spaces
|
| 5 |
|
| 6 |
+
# Dictionary to store loaded models and tokenizers
|
| 7 |
+
loaded_models = {}
|
| 8 |
+
|
| 9 |
+
def load_model(model_name):
|
| 10 |
+
"""Load the model and tokenizer if not already loaded."""
|
| 11 |
+
if model_name not in loaded_models:
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_name, torch_dtype=torch.float16, device_map="auto"
|
| 15 |
+
)
|
| 16 |
+
loaded_models[model_name] = (tokenizer, model)
|
| 17 |
+
return loaded_models[model_name]
|
| 18 |
+
|
| 19 |
+
@spaces.GPU
|
| 20 |
+
def generate_text(model_name, prompt):
|
| 21 |
+
"""Generate text using the selected model."""
|
| 22 |
+
tokenizer, model = load_model(model_name)
|
| 23 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 24 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
| 25 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
# List of models to choose from
|
| 28 |
+
model_choices = [
|
| 29 |
+
"deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
| 30 |
+
"meta-llama/Llama-3.2-3B-Instruct",
|
| 31 |
+
"google/gemma-7b"
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Gradio interface setup
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## Clinical Text Analysis with Multiple Models")
|
| 37 |
+
model_selector = gr.Dropdown(choices=model_choices, label="Select Model")
|
| 38 |
+
input_text = gr.Textbox(label="Input Clinical Text")
|
| 39 |
+
output_text = gr.Textbox(label="Generated Output")
|
| 40 |
+
analyze_button = gr.Button("Analyze")
|
| 41 |
+
|
| 42 |
+
analyze_button.click(fn=generate_text, inputs=[model_selector, input_text], outputs=output_text)
|
| 43 |
|
|
|
|
| 44 |
demo.launch()
|