abdullahzunorain
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,48 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
import
|
4 |
-
|
5 |
-
# Set
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Step 1: Set up Bio_ClinicalBERT model for medical text generation
|
8 |
+
device = 0 if torch.cuda.is_available() else -1 # Set to 0 for GPU, -1 for CPU
|
9 |
+
model_name = "emilyalsentzer/Bio_ClinicalBERT" # Bio_ClinicalBERT model for medical context
|
10 |
+
|
11 |
+
# Load tokenizer and model
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
+
model = AutoModelForMaskedLM.from_pretrained(model_name)
|
14 |
+
|
15 |
+
# Create a text generation pipeline with the loaded model
|
16 |
+
chatbot = pipeline(
|
17 |
+
"text-generation",
|
18 |
+
model=model,
|
19 |
+
tokenizer=tokenizer,
|
20 |
+
device=device,
|
21 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
22 |
+
)
|
23 |
+
|
24 |
+
# Step 2: Function to generate chatbot responses
|
25 |
+
def get_response(user_input):
|
26 |
+
response = chatbot(
|
27 |
+
user_input,
|
28 |
+
max_length=75, # Adjusted to manage memory usage
|
29 |
+
num_return_sequences=1,
|
30 |
+
truncation=True
|
31 |
+
)
|
32 |
+
return response[0]['generated_text']
|
33 |
+
|
34 |
+
# Step 3: Create Gradio interface
|
35 |
+
def chatbot_interface(user_input):
|
36 |
+
return get_response(user_input)
|
37 |
+
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=chatbot_interface,
|
40 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms here..."),
|
41 |
+
outputs="text",
|
42 |
+
title="Health Chatbot",
|
43 |
+
description="Ask your symptoms and get advice!",
|
44 |
+
theme="default"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Step 4: Launch the Gradio app
|
48 |
+
iface.launch()
|