krish10 commited on
Commit
67e0693
·
verified ·
1 Parent(s): b69b123

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import spaces
3
  import torch
4
  import os
5
- from transformers import AutoTokenizer, AutoModelForCausalLM, StoppingCriteria, StoppingCriteriaList
6
 
7
  # Load model from secret
8
  model_name = os.environ.get("MODEL_ID")
@@ -17,11 +17,6 @@ model.eval()
17
 
18
  SYSTEM_PROMPT = "You are an expert biomedical assistant trained to identify randomized controlled trials (RCTs). Include RCTs and exclude non-RCTs."
19
 
20
- class StopOnTokens(StoppingCriteria):
21
- def __call__(self, input_ids, scores, **kwargs):
22
- # stop generation when EOS is reached
23
- return input_ids[0][-1] in [tokenizer.eos_token_id]
24
-
25
  @spaces.GPU(duration=120)
26
  def stream_response(title_text, abstract_text):
27
  user_input = f"Title: {title_text.strip()}\nAbstract: {abstract_text.strip()}"
@@ -32,8 +27,9 @@ def stream_response(title_text, abstract_text):
32
 
33
  generated = inputs["input_ids"]
34
  past_key_values = None
 
35
 
36
- for _ in range(1024): # max tokens
37
  outputs = model(input_ids=generated, past_key_values=past_key_values, use_cache=True)
38
  next_token_logits = outputs.logits[:, -1, :]
39
  next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)
@@ -43,31 +39,30 @@ def stream_response(title_text, abstract_text):
43
 
44
  generated = torch.cat((generated, next_token), dim=1)
45
  decoded_output = tokenizer.decode(generated[0], skip_special_tokens=True)
46
- # Stream only the assistant's reply
47
  if "<|assistant|>" in decoded_output:
48
- reply = decoded_output.split("<|assistant|>")[-1].strip()
49
- yield reply
50
 
51
  with gr.Blocks() as demo:
52
- gr.Markdown("## 🧠 RCT Classifier Demonstration (Streaming)")
53
 
54
  chatbot = gr.Chatbot()
55
-
56
  with gr.Row():
57
- title = gr.Textbox(label="Title", placeholder="Enter article title")
58
  abstract = gr.Textbox(label="Abstract", placeholder="Enter abstract", lines=6)
59
-
60
  submit = gr.Button("Classify")
61
 
62
- def wrapper(title, abstract):
63
- message = f"Title: {title.strip()}\nAbstract: {abstract.strip()}"
64
- return [(message, stream_response(title, abstract))] # yield response
 
 
 
 
 
65
 
66
- submit.click(
67
- fn=wrapper,
68
- inputs=[title, abstract],
69
- outputs=chatbot
70
- )
71
 
72
  if __name__ == "__main__":
73
- demo.launch()
 
2
  import spaces
3
  import torch
4
  import os
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
  # Load model from secret
8
  model_name = os.environ.get("MODEL_ID")
 
17
 
18
  SYSTEM_PROMPT = "You are an expert biomedical assistant trained to identify randomized controlled trials (RCTs). Include RCTs and exclude non-RCTs."
19
 
 
 
 
 
 
20
  @spaces.GPU(duration=120)
21
  def stream_response(title_text, abstract_text):
22
  user_input = f"Title: {title_text.strip()}\nAbstract: {abstract_text.strip()}"
 
27
 
28
  generated = inputs["input_ids"]
29
  past_key_values = None
30
+ response_text = ""
31
 
32
+ for _ in range(1024): # limit max length
33
  outputs = model(input_ids=generated, past_key_values=past_key_values, use_cache=True)
34
  next_token_logits = outputs.logits[:, -1, :]
35
  next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)
 
39
 
40
  generated = torch.cat((generated, next_token), dim=1)
41
  decoded_output = tokenizer.decode(generated[0], skip_special_tokens=True)
42
+
43
  if "<|assistant|>" in decoded_output:
44
+ response_text = decoded_output.split("<|assistant|>")[-1].strip()
45
+ yield response_text
46
 
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("## 🧠 RCT Classifier Demonstration (Streaming Enabled)")
49
 
50
  chatbot = gr.Chatbot()
 
51
  with gr.Row():
52
+ title = gr.Textbox(label="Title", placeholder="Enter title")
53
  abstract = gr.Textbox(label="Abstract", placeholder="Enter abstract", lines=6)
 
54
  submit = gr.Button("Classify")
55
 
56
+ def stream_chat(title_text, abstract_text):
57
+ user_message = f"Title: {title_text.strip()}\nAbstract: {abstract_text.strip()}"
58
+ yield (user_message, "") # show user message
59
+ response_stream = stream_response(title_text, abstract_text)
60
+ collected = ""
61
+ for partial in response_stream:
62
+ collected = partial
63
+ yield (user_message, collected)
64
 
65
+ submit.stream(fn=stream_chat, inputs=[title, abstract], outputs=chatbot)
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
+ demo.launch()