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