Tarive commited on
Commit
853c001
·
verified ·
1 Parent(s): 77c768e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+
3
+ import os
4
+
5
+ import gradio as gr
6
+
7
+ import keras
8
+
9
+ import keras_hub
10
+
11
+ from huggingface_hub import from_pretrained_keras
12
+
13
+
14
+
15
+ # Set Keras backend
16
+
17
+ os.environ["KERAS_BACKEND"] = "jax"
18
+
19
+ os.environ["XLA_PYTHON_CLIENT_MEM_FRACTION"] = "1.00"
20
+
21
+
22
+
23
+ # --- 1. LOAD THE MERGED MODEL FROM THE HUB ---
24
+
25
+ # !!! IMPORTANT: Change this to your username and repo name !!!
26
+
27
+ repo_id = "Tarive/lora_research_abstracts" 
28
+
29
+ print(f"Loading merged model from Hub: {repo_id}")
30
+
31
+ gemma_lm = from_pretrained_keras(repo_id)
32
+
33
+
34
+
35
+ # Compile the model with a sampler for generation
36
+
37
+ gemma_lm.compile(sampler=keras_hub.samplers.TopKSampler(k=5))
38
+
39
+ print("Model loaded and compiled successfully.")
40
+
41
+
42
+
43
+
44
+
45
+ # --- 2. DEFINE THE INFERENCE FUNCTION ---
46
+
47
+ def revise_abstract(draft_abstract, grant_type):
48
+
49
+     if not draft_abstract or not grant_type:
50
+
51
+         return "Error: Please provide both a draft abstract and a grant type."
52
+
53
+
54
+
55
+     template = (
56
+
57
+         "Instruction:\n"
58
+
59
+         "You are an expert grant writer. Rewrite the following draft abstract to be more impactful and clear, "
60
+
61
+         "following the specific conventions of a {activity_code} grant. Ensure the most compelling claims are front-loaded.\n\n"
62
+
63
+         "Input Draft:\n"
64
+
65
+         "{unoptimized_abstract}\n\n"
66
+
67
+         "Revised Abstract:"
68
+
69
+     )
70
+
71
+     prompt = template.format(unoptimized_abstract=draft_abstract, activity_code=grant_type)
72
+
73
+     output = gemma_lm.generate(prompt, max_length=1024)
74
+
75
+     
76
+
77
+     parts = output.split("Revised Abstract:")
78
+
79
+     return parts[1].strip() if len(parts) > 1 else output.strip()
80
+
81
+
82
+
83
+ # --- 3. CREATE THE GRADIO INTERFACE ---
84
+
85
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
+
87
+     gr.Markdown("# Grant Abstract Revision Tool (Fine-Tuned on Gemma)")
88
+
89
+     gr.Markdown("Enter a draft abstract and select its grant type. The model will rewrite it to be more impactful, based on patterns from successfully funded NIH grants.")
90
+
91
+     
92
+
93
+     with gr.Row():
94
+
95
+         draft_input = gr.Textbox(lines=15, label="Input Draft Abstract", placeholder="Paste your draft abstract here...")
96
+
97
+         grant_type_input = gr.Dropdown(
98
+
99
+             ["R01", "R21", "F32", "T32", "P30", "R41", "R43", "R44", "K99"], 
100
+
101
+             label="Grant Type (Activity Code)",
102
+
103
+             info="Select the grant mechanism you are targeting."
104
+
105
+         )
106
+
107
+     
108
+
109
+     submit_button = gr.Button("Revise Abstract", variant="primary")
110
+
111
+     revised_output = gr.Textbox(lines=15, label="Model's Revised Abstract", interactive=False)
112
+
113
+     
114
+
115
+     submit_button.click(fn=revise_abstract, inputs=[draft_input, grant_type_input], outputs=revised_output)
116
+
117
+     
118
+
119
+     gr.Examples(
120
+
121
+         examples=[
122
+
123
+             ["SUMMARY \nA pressing concern exists regarding lead poisoning...This study aimed to optimize and validate a dried blood spot collection device...", "R41"],
124
+
125
+             ["This project is about figuring out how macrophages and S. flexneri interact...Our study will look at this in vitro and in vivo...", "R21"]
126
+
127
+         ],
128
+
129
+         inputs=[draft_input, grant_type_input]
130
+
131
+     )
132
+
133
+
134
+
135
+ print("Launching Gradio app...")
136
+
137
+ demo.launch()