nafisneehal
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,75 +4,58 @@ import torch
|
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
import spaces
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
9 |
model_name = "linjc16/Panacea-7B-Chat"
|
10 |
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
11 |
model.to(device)
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
{"role": "user", "content": user_input},
|
23 |
-
]
|
24 |
-
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt").to(device)
|
25 |
-
model_inputs = encodeds.to(device)
|
26 |
|
27 |
# Generate model response
|
28 |
with torch.no_grad():
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
saved_prompts[prompt_name] = {"system": system_instruction, "user": user_input}
|
34 |
-
return response, list(saved_prompts.keys())
|
35 |
-
|
36 |
-
def load_prompt(prompt_name):
|
37 |
-
# Load selected prompt into the input fields
|
38 |
-
if prompt_name in saved_prompts:
|
39 |
-
prompt = saved_prompts[prompt_name]
|
40 |
-
return prompt["system"], prompt["user"]
|
41 |
-
return "", ""
|
42 |
|
43 |
-
|
44 |
-
# Rename a saved prompt
|
45 |
-
if old_name in saved_prompts:
|
46 |
-
saved_prompts[new_name] = saved_prompts.pop(old_name)
|
47 |
-
return list(saved_prompts.keys())
|
48 |
|
49 |
-
# Gradio interface
|
50 |
with gr.Blocks() as demo:
|
51 |
-
gr.Markdown("# Clinical Trial Chatbot
|
52 |
|
53 |
with gr.Row():
|
54 |
-
# Left
|
55 |
with gr.Column():
|
56 |
-
saved_prompts_list = gr.Dropdown(label="Saved Prompts", choices=[])
|
57 |
-
rename_input = gr.Textbox(label="Rename Prompt")
|
58 |
-
rename_button = gr.Button("Rename")
|
59 |
system_instruction = gr.Textbox(
|
60 |
placeholder="Enter system instruction here...", label="System Instruction")
|
61 |
user_input = gr.Textbox(
|
62 |
placeholder="Type your message here...", label="Your Message")
|
63 |
-
|
64 |
-
submit_btn = gr.Button("Save & Submit")
|
65 |
|
66 |
-
# Right column for displaying bot response
|
67 |
with gr.Column():
|
68 |
response_display = gr.Textbox(
|
69 |
label="Bot Response", interactive=False, placeholder="Response will appear here.")
|
70 |
|
71 |
-
# Link
|
72 |
-
submit_btn.click(generate_response, [system_instruction, user_input
|
73 |
-
[response_display, saved_prompts_list])
|
74 |
-
saved_prompts_list.change(load_prompt, saved_prompts_list, [system_instruction, user_input])
|
75 |
-
rename_button.click(rename_prompt, [saved_prompts_list, rename_input], saved_prompts_list)
|
76 |
|
77 |
# Launch the app
|
78 |
-
demo.launch()
|
|
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
import spaces
|
6 |
|
7 |
+
# Check if we're running in a Hugging Face Space with zero GPU constraints
|
8 |
+
IS_SPACES_ZERO = os.environ.get("SPACES_ZERO_GPU", "0") == "1"
|
9 |
+
IS_SPACE = os.environ.get("SPACE_ID", None) is not None
|
10 |
+
|
11 |
+
# Determine device (set to CPU for zero-GPU)
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
print(f"Using device: {device}")
|
14 |
+
|
15 |
+
# Load model and tokenizer
|
16 |
model_name = "linjc16/Panacea-7B-Chat"
|
17 |
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
18 |
model.to(device)
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
20 |
|
21 |
+
# Define prompt structure
|
22 |
+
@spaces.GPU # This will handle spaces for either GPU or CPU as available
|
23 |
+
def generate_response(system_instruction, user_input):
|
24 |
+
# Format the prompt with the system instruction and user input
|
25 |
+
prompt = f"{system_instruction}\n\nUser: {user_input}\nBot:"
|
26 |
+
|
27 |
+
# Tokenize and prepare inputs
|
28 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Generate model response
|
31 |
with torch.no_grad():
|
32 |
+
outputs = model.generate(**inputs, max_new_tokens=100, do_sample=True)
|
33 |
+
|
34 |
+
# Decode response
|
35 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Bot:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
return response # Return only bot's response for display in the right column
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Set up Gradio interface
|
40 |
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("# Clinical Trial Chatbot")
|
42 |
|
43 |
with gr.Row():
|
44 |
+
# Left column for inputs
|
45 |
with gr.Column():
|
|
|
|
|
|
|
46 |
system_instruction = gr.Textbox(
|
47 |
placeholder="Enter system instruction here...", label="System Instruction")
|
48 |
user_input = gr.Textbox(
|
49 |
placeholder="Type your message here...", label="Your Message")
|
50 |
+
submit_btn = gr.Button("Submit")
|
|
|
51 |
|
52 |
+
# Right column for displaying bot's response
|
53 |
with gr.Column():
|
54 |
response_display = gr.Textbox(
|
55 |
label="Bot Response", interactive=False, placeholder="Response will appear here.")
|
56 |
|
57 |
+
# Link the submit button to the generate_response function
|
58 |
+
submit_btn.click(generate_response, [system_instruction, user_input], response_display)
|
|
|
|
|
|
|
59 |
|
60 |
# Launch the app
|
61 |
+
demo.launch(share=True)
|