krish10 commited on
Commit
73f480d
Β·
verified Β·
1 Parent(s): d7fea83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -58
app.py CHANGED
@@ -1,10 +1,9 @@
 
1
  import gradio as gr
2
  from transformers import pipeline, TextIteratorStreamer
3
  import torch
4
  import threading
5
- import spaces
6
 
7
- # --- Model and Tokenizer Setup ---
8
  # Load model and tokenizer
9
  model_name = "krish10/Qwen3_0.6B_16bit_TA_screen"
10
  pipe = pipeline("text-generation", model=model_name, device=0)
@@ -16,12 +15,11 @@ MAX_TOKENS = 3000
16
  TEMPERATURE = 0.1
17
  TOP_P = 0.9
18
 
19
- # --- Backend Function ---
20
  @spaces.GPU
21
  def respond_stream(population, intervention, comparison, outcome, study_design, summary, title, abstract):
22
  # Validate required fields
23
  if not title.strip() or not abstract.strip():
24
- return "❌ **Error:** Title and Abstract are required fields."
25
 
26
  criteria_parts = []
27
  if population.strip():
@@ -35,9 +33,8 @@ def respond_stream(population, intervention, comparison, outcome, study_design,
35
  if study_design.strip():
36
  criteria_parts.append(f"Study design of interest = {study_design.strip()}")
37
 
38
- # Ensure at least one PICOS criteria is provided if no summary exists
39
- if not criteria_parts and not summary.strip():
40
- return "❌ **Error:** You must fill out at least one of the five PICOS criteria or provide a PICO Summary."
41
 
42
  # Build instruction section
43
  instruction = "Instruction: " + "\n".join(criteria_parts)
@@ -74,62 +71,31 @@ def respond_stream(population, intervention, comparison, outcome, study_design,
74
  partial_text += token
75
  yield partial_text
76
 
77
- # --- Modernized Gradio UI ---
78
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky")) as demo:
79
- gr.Markdown("# πŸ€– Qwen Streaming Chat: Structured Medical Abstract Evaluation")
80
- gr.Markdown("Provide a study's title and abstract, and optionally add PICOS criteria to guide the model's analysis.")
81
-
82
- with gr.Row(equal_height=False):
83
- # Left Column: Main inputs (Title and Abstract)
84
- with gr.Column(scale=2):
85
- title = gr.Textbox(
86
- label="Study Title",
87
- lines=2,
88
- placeholder="Enter the title of the medical study here...",
89
- info="This field is required."
90
- )
91
- abstract = gr.Textbox(
92
- label="Study Abstract",
93
- lines=10,
94
- placeholder="Paste the full abstract here...",
95
- info="This field is required."
96
- )
97
-
98
- # Right Column: Optional PICOS criteria
99
- with gr.Column(scale=1):
100
- with gr.Accordion("Define PICOS Criteria (Optional)", open=False):
101
- population = gr.Textbox(label="Population", info="e.g., 'patients with type 2 diabetes'")
102
- intervention = gr.Textbox(label="Intervention", info="e.g., 'metformin'")
103
- comparison = gr.Textbox(label="Comparison", info="e.g., 'placebo'")
104
- outcome = gr.Textbox(label="Outcome", info="e.g., 'HbA1c levels'")
105
- study_design = gr.Textbox(label="Study Design", info="e.g., 'randomized controlled trial'")
106
-
107
- summary = gr.Textbox(
108
- label="PICO Summary (Optional)",
109
- lines=4,
110
- placeholder="Alternatively, describe the PICOS criteria in a short paragraph.",
111
- )
112
-
113
- # Output and Action Buttons
114
- output_box = gr.Markdown(label="Model Response") # CORRECTED LINE
115
-
116
- with gr.Row():
117
- clear_btn = gr.Button("Clear")
118
- generate_btn = gr.Button("Generate", variant="primary")
119
-
120
- # --- Button Click Handlers ---
121
- all_inputs = [population, intervention, comparison, outcome, study_design, summary, title, abstract]
122
 
123
  generate_btn.click(
124
  fn=respond_stream,
125
- inputs=all_inputs,
126
  outputs=[output_box]
127
  )
128
-
129
- # The clear button resets all input fields and the output box
130
- clear_btn.click(lambda: (None, None, None, None, None, None, None, None, None), outputs=all_inputs + [output_box])
131
-
132
 
133
- # --- Launch the App ---
134
  if __name__ == "__main__":
135
  demo.launch()
 
1
+ import spaces
2
  import gradio as gr
3
  from transformers import pipeline, TextIteratorStreamer
4
  import torch
5
  import threading
 
6
 
 
7
  # Load model and tokenizer
8
  model_name = "krish10/Qwen3_0.6B_16bit_TA_screen"
9
  pipe = pipeline("text-generation", model=model_name, device=0)
 
15
  TEMPERATURE = 0.1
16
  TOP_P = 0.9
17
 
 
18
  @spaces.GPU
19
  def respond_stream(population, intervention, comparison, outcome, study_design, summary, title, abstract):
20
  # Validate required fields
21
  if not title.strip() or not abstract.strip():
22
+ return "❌ Error: Title and Abstract are required."
23
 
24
  criteria_parts = []
25
  if population.strip():
 
33
  if study_design.strip():
34
  criteria_parts.append(f"Study design of interest = {study_design.strip()}")
35
 
36
+ if not criteria_parts:
37
+ return "❌ Error: At least one of the five PICOS criteria must be filled."
 
38
 
39
  # Build instruction section
40
  instruction = "Instruction: " + "\n".join(criteria_parts)
 
71
  partial_text += token
72
  yield partial_text
73
 
74
+ # Build Gradio interface
75
+ with gr.Blocks() as demo:
76
+ gr.Markdown("## πŸ€– Qwen Streaming Chat β€” Structured Medical Abstract Evaluation")
77
+
78
+ with gr.Column():
79
+ population = gr.Textbox(label="Population of interest", lines=1)
80
+ intervention = gr.Textbox(label="Intervention/exposure of interest", lines=1)
81
+ comparison = gr.Textbox(label="Comparison of interest", lines=1)
82
+ outcome = gr.Textbox(label="Outcome of interest", lines=1)
83
+ study_design = gr.Textbox(label="Study design of interest", lines=1)
84
+
85
+ with gr.Column():
86
+ summary = gr.Textbox(label="PICOS Summary (optional)", lines=4)
87
+ title = gr.Textbox(label="Title", lines=2, placeholder="Required")
88
+ abstract = gr.Textbox(label="Abstract", lines=10, placeholder="Required")
89
+
90
+ output_box = gr.Textbox(label="Model Response", lines=15, interactive=False)
91
+ generate_btn = gr.Button("Generate")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  generate_btn.click(
94
  fn=respond_stream,
95
+ inputs=[population, intervention, comparison, outcome, study_design, summary, title, abstract],
96
  outputs=[output_box]
97
  )
 
 
 
 
98
 
99
+ # Launch the app
100
  if __name__ == "__main__":
101
  demo.launch()