nafisneehal commited on
Commit
39c823d
·
verified ·
1 Parent(s): b29b7b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -25
app.py CHANGED
@@ -1,47 +1,38 @@
1
  import gradio as gr
2
- import os
3
  import torch
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")
@@ -49,12 +40,12 @@ with gr.Blocks() as demo:
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
 
1
  import gradio as gr
 
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import spaces
5
 
6
+ # Initialize model
 
 
 
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
8
  model_name = "linjc16/Panacea-7B-Chat"
9
  model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
10
  model.to(device)
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
 
13
+ @spaces.GPU
 
14
  def generate_response(system_instruction, user_input):
15
+ # Format the prompt using the messages structure
16
+ messages = [
17
+ {"role": "system", "content": system_instruction},
18
+ {"role": "user", "content": user_input},
19
+ ]
20
+ encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt").to(device)
21
+ model_inputs = encodeds.to(device)
22
 
23
  # Generate model response
24
  with torch.no_grad():
25
+ generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
26
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].split("Bot:")[-1].strip()
 
 
27
 
28
+ return response
29
 
30
+ # Gradio interface setup
31
  with gr.Blocks() as demo:
32
  gr.Markdown("# Clinical Trial Chatbot")
33
 
34
  with gr.Row():
35
+ # Left sidebar for inputs
36
  with gr.Column():
37
  system_instruction = gr.Textbox(
38
  placeholder="Enter system instruction here...", label="System Instruction")
 
40
  placeholder="Type your message here...", label="Your Message")
41
  submit_btn = gr.Button("Submit")
42
 
43
+ # Right column for displaying bot response
44
  with gr.Column():
45
  response_display = gr.Textbox(
46
  label="Bot Response", interactive=False, placeholder="Response will appear here.")
47
 
48
+ # Link submit button to the generate_response function
49
  submit_btn.click(generate_response, [system_instruction, user_input], response_display)
50
 
51
  # Launch the app