Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,94 @@
|
|
| 1 |
-
import random
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
|
| 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
MODEL_NAME = "docto/Docto-Bot"
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and model
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 10 |
+
|
| 11 |
+
# Use GPU if available
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
model.to(device)
|
| 14 |
+
|
| 15 |
+
# Set pad token if missing
|
| 16 |
+
if tokenizer.pad_token is None:
|
| 17 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_reply(user_input):
|
| 21 |
+
|
| 22 |
+
if not user_input.strip():
|
| 23 |
+
return "Please enter a question."
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Build prompt
|
| 27 |
+
prompt = f"Question: {user_input}\nAnswer:"
|
| 28 |
+
|
| 29 |
+
# Tokenize
|
| 30 |
+
inputs = tokenizer(
|
| 31 |
+
prompt,
|
| 32 |
+
return_tensors="pt"
|
| 33 |
+
).to(device)
|
| 34 |
+
|
| 35 |
+
# Generate response
|
| 36 |
+
outputs = model.generate(
|
| 37 |
+
**inputs,
|
| 38 |
+
|
| 39 |
+
max_new_tokens=150,
|
| 40 |
+
|
| 41 |
+
do_sample=True,
|
| 42 |
+
|
| 43 |
+
temperature=0.7,
|
| 44 |
+
|
| 45 |
+
top_k=50,
|
| 46 |
+
|
| 47 |
+
top_p=0.9,
|
| 48 |
+
|
| 49 |
+
repetition_penalty=1.15,
|
| 50 |
+
|
| 51 |
+
no_repeat_ngram_size=3,
|
| 52 |
+
|
| 53 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 54 |
+
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Decode
|
| 59 |
+
response = tokenizer.decode(
|
| 60 |
+
outputs[0],
|
| 61 |
+
skip_special_tokens=True
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Extract answer only
|
| 65 |
+
if "Answer:" in response:
|
| 66 |
+
response = response.split("Answer:", 1)[1]
|
| 67 |
+
|
| 68 |
+
return response.strip()
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error: {e}"
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# Gradio UI
|
| 75 |
+
iface = gr.Interface(
|
| 76 |
+
fn=get_reply,
|
| 77 |
+
|
| 78 |
+
inputs=gr.Textbox(
|
| 79 |
+
lines=2,
|
| 80 |
+
placeholder="Ask a question..."
|
| 81 |
+
),
|
| 82 |
+
|
| 83 |
+
outputs=gr.Textbox(
|
| 84 |
+
label="Bot Response"
|
| 85 |
+
),
|
| 86 |
+
|
| 87 |
+
title="Docto-Bot",
|
| 88 |
+
|
| 89 |
+
description="Medical Question Answering Bot",
|
| 90 |
+
|
| 91 |
+
allow_flagging="never"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
iface.launch()
|