Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,52 @@ from typing import List, Dict, Tuple
|
|
| 8 |
# Initialize Groq client
|
| 9 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def analyze_writing_style(text: str) -> Dict[str, float]:
|
| 12 |
"""Analyze writing style metrics from text"""
|
| 13 |
sentences = re.split(r'[.!?]+', text)
|
|
@@ -115,6 +161,80 @@ def build_style_profile_and_rewrite(abstracts: str, original_text: str) -> Tuple
|
|
| 115 |
except Exception as e:
|
| 116 |
return f"Error: {str(e)}", "{}"
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def create_deployment_instructions():
|
| 119 |
"""Create deployment instructions for Hugging Face Spaces"""
|
| 120 |
return """
|
|
@@ -173,6 +293,7 @@ def create_deployment_instructions():
|
|
| 173 |
with gr.Blocks(title="StyleForge Lite", theme=gr.themes.Soft()) as app:
|
| 174 |
gr.Markdown("# 🔬 StyleForge Lite")
|
| 175 |
gr.Markdown("Learn your scientific writing style and rewrite text using AI")
|
|
|
|
| 176 |
|
| 177 |
with gr.Tab("Style Analysis & Rewrite"):
|
| 178 |
with gr.Row():
|
|
@@ -191,9 +312,16 @@ with gr.Blocks(title="StyleForge Lite", theme=gr.themes.Soft()) as app:
|
|
| 191 |
max_lines=12
|
| 192 |
)
|
| 193 |
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
rewritten_output = gr.Textbox(
|
| 198 |
label="✨ Rewritten Text",
|
| 199 |
lines=12,
|
|
@@ -214,6 +342,18 @@ with gr.Blocks(title="StyleForge Lite", theme=gr.themes.Soft()) as app:
|
|
| 214 |
inputs=[abstracts_input, original_text_input],
|
| 215 |
outputs=[rewritten_output, metrics_output]
|
| 216 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
# Add examples
|
| 219 |
gr.Examples(
|
|
|
|
| 8 |
# Initialize Groq client
|
| 9 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 10 |
|
| 11 |
+
# Built-in style guide and examples (used when user does not paste abstracts)
|
| 12 |
+
DEFAULT_STYLE_GUIDE = """
|
| 13 |
+
You are a scientific writing assistant that mimics the writing style of Dr. Babajan Banaganapalli.
|
| 14 |
+
|
| 15 |
+
STYLE RULES
|
| 16 |
+
|
| 17 |
+
- Formal, confident scientific tone.
|
| 18 |
+
- Medium–long sentences (≈15–25 words).
|
| 19 |
+
- Clear flow: ① brief context → ② method/approach → ③ results → ④ implication.
|
| 20 |
+
- Use precise verbs (identify, demonstrate, predict, suggest, support).
|
| 21 |
+
- Use technical terms naturally; avoid unnecessary jargon.
|
| 22 |
+
- Interpret cautiously (e.g., “suggest,” “support,” “warrant further study”).
|
| 23 |
+
- Prefer active voice; use passive only for emphasis.
|
| 24 |
+
- Keep paragraphs compact (1–3 sentences each).
|
| 25 |
+
|
| 26 |
+
TASK RULES
|
| 27 |
+
|
| 28 |
+
- When asked to “Analyze style,” first output a 10-bullet style profile.
|
| 29 |
+
- When asked to “Rewrite,” keep all facts and numbers; only change wording and rhythm to match the style.
|
| 30 |
+
- If citations are present, preserve them. If acronyms appear, define once, then reuse.
|
| 31 |
+
|
| 32 |
+
EXAMPLES—LEARN ONLY
|
| 33 |
+
Example 1
|
| 34 |
+
“Resveratrol (RESL), a natural polyphenol, has been studied for cancer chemoprevention.
|
| 35 |
+
In this study, a diacetate derivative (RESL43) was synthesized, showing potent cytotoxicity and apoptosis induction in U937 cells.
|
| 36 |
+
Molecular docking indicated that RESL43 may inhibit NFkB by disrupting DNA–protein interactions.
|
| 37 |
+
These findings support stronger activity than RESL and warrant further investigation.”
|
| 38 |
+
|
| 39 |
+
Example 2
|
| 40 |
+
“Tuberculosis remains a leading infectious disease worldwide.
|
| 41 |
+
Here, we developed the first 3D structural model of Mtb-MurA using homology modeling and molecular dynamics.
|
| 42 |
+
Docking studies revealed that 5-sulfonoxyanthranilic acid derivatives showed the best interaction with Mtb-MurA.
|
| 43 |
+
This suggests potential for designing specific inhibitors targeting the bacterial enzyme.”
|
| 44 |
+
|
| 45 |
+
Example 3
|
| 46 |
+
“The MED12 gene encodes a transcriptional regulator implicated in developmental disorders and cancers.
|
| 47 |
+
Through homology modeling and in silico mutational analysis, we predicted deleterious missense variants impacting stability and function.
|
| 48 |
+
Findings indicate that MED12 mutations may disrupt Mediator complex function, demonstrating the utility of computational phenotype prediction.”
|
| 49 |
+
|
| 50 |
+
Example 4
|
| 51 |
+
“Celiac disease is a complex genetic disorder.
|
| 52 |
+
Using a multidimensional computational approach integrating eQTL, histone marks, and TF binding, we prioritized CCR2 as a candidate gene.
|
| 53 |
+
Functional annotation suggested that risk SNPs modulate CCR2 expression in immune tissues, supporting its role in CD biology.
|
| 54 |
+
These results highlight CCR2 as a candidate biomarker and therapeutic target.”
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
def analyze_writing_style(text: str) -> Dict[str, float]:
|
| 58 |
"""Analyze writing style metrics from text"""
|
| 59 |
sentences = re.split(r'[.!?]+', text)
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
return f"Error: {str(e)}", "{}"
|
| 163 |
|
| 164 |
+
def analyze_style_only(abstracts: str) -> Tuple[str, str, str]:
|
| 165 |
+
"""Analyze style from abstracts only and return profile markdown, metrics json, and raw profile string for state."""
|
| 166 |
+
source_corpus = abstracts.strip() if abstracts.strip() else DEFAULT_STYLE_GUIDE
|
| 167 |
+
|
| 168 |
+
# Produce a concise 10-point style profile using Groq
|
| 169 |
+
prompt = f"""
|
| 170 |
+
Developer: You are a scientific writing assistant designed to emulate the style of Dr. Babajan Banaganapalli.
|
| 171 |
+
|
| 172 |
+
TASK RULES
|
| 173 |
+
- Begin with a concise checklist (3–7 bullets) of what you will do; keep items conceptual.
|
| 174 |
+
- Upon receiving a request to “Analyze style,” first provide a 10-point style profile in bullet format.
|
| 175 |
+
|
| 176 |
+
First Prompt: Analyze the embedded examples and output a 10-bullet style profile.
|
| 177 |
+
Then say: READY FOR REWRITES.
|
| 178 |
+
|
| 179 |
+
Training corpus (analyze this):
|
| 180 |
+
{source_corpus}
|
| 181 |
+
|
| 182 |
+
Output strictly:
|
| 183 |
+
1) A 3–7 bullet checklist of the conceptual steps you will take.
|
| 184 |
+
2) A 10-point style profile in bullets.
|
| 185 |
+
3) A final line: READY FOR REWRITES.
|
| 186 |
+
"""
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
completion = client.chat.completions.create(
|
| 190 |
+
model="openai/gpt-oss-120b",
|
| 191 |
+
messages=[{"role": "user", "content": prompt}],
|
| 192 |
+
temperature=0.5,
|
| 193 |
+
max_completion_tokens=1200,
|
| 194 |
+
top_p=1,
|
| 195 |
+
reasoning_effort="medium"
|
| 196 |
+
)
|
| 197 |
+
profile_text = completion.choices[0].message.content or ""
|
| 198 |
+
# Compute metrics on the chosen corpus as a proxy
|
| 199 |
+
metrics = analyze_writing_style(source_corpus)
|
| 200 |
+
return profile_text, json.dumps(metrics, indent=2), profile_text
|
| 201 |
+
except Exception as e:
|
| 202 |
+
return f"Error: {str(e)}", "{}", ""
|
| 203 |
+
|
| 204 |
+
def rewrite_with_profile(original_text: str, style_profile: str) -> Tuple[str, str]:
|
| 205 |
+
"""Rewrite using a previously analyzed style profile."""
|
| 206 |
+
if not style_profile.strip():
|
| 207 |
+
return "No saved style profile. Please click 'Analyze Style' first.", "{}"
|
| 208 |
+
if not original_text.strip():
|
| 209 |
+
return "Please provide text to rewrite.", "{}"
|
| 210 |
+
|
| 211 |
+
try:
|
| 212 |
+
style_prompt = f"""
|
| 213 |
+
Developer instructions and STYLE RULES apply.
|
| 214 |
+
|
| 215 |
+
STYLE PROFILE (learned from user abstracts):
|
| 216 |
+
{style_profile}
|
| 217 |
+
|
| 218 |
+
Rewrite the following text to match the STYLE PROFILE exactly, preserving all facts, numbers, and citations. Define acronyms on first mention.
|
| 219 |
+
|
| 220 |
+
Text:
|
| 221 |
+
{original_text}
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
completion = client.chat.completions.create(
|
| 225 |
+
model="openai/gpt-oss-120b",
|
| 226 |
+
messages=[{"role": "user", "content": style_prompt}],
|
| 227 |
+
temperature=0.7,
|
| 228 |
+
max_completion_tokens=2048,
|
| 229 |
+
top_p=1,
|
| 230 |
+
reasoning_effort="medium"
|
| 231 |
+
)
|
| 232 |
+
rewritten_text = completion.choices[0].message.content or ""
|
| 233 |
+
metrics = analyze_writing_style(rewritten_text)
|
| 234 |
+
return rewritten_text, json.dumps(metrics, indent=2)
|
| 235 |
+
except Exception as e:
|
| 236 |
+
return f"Error: {str(e)}", "{}"
|
| 237 |
+
|
| 238 |
def create_deployment_instructions():
|
| 239 |
"""Create deployment instructions for Hugging Face Spaces"""
|
| 240 |
return """
|
|
|
|
| 293 |
with gr.Blocks(title="StyleForge Lite", theme=gr.themes.Soft()) as app:
|
| 294 |
gr.Markdown("# 🔬 StyleForge Lite")
|
| 295 |
gr.Markdown("Learn your scientific writing style and rewrite text using AI")
|
| 296 |
+
style_profile_state = gr.State("")
|
| 297 |
|
| 298 |
with gr.Tab("Style Analysis & Rewrite"):
|
| 299 |
with gr.Row():
|
|
|
|
| 312 |
max_lines=12
|
| 313 |
)
|
| 314 |
|
| 315 |
+
with gr.Row():
|
| 316 |
+
analyze_only_btn = gr.Button("🧪 Analyze Style", variant="secondary")
|
| 317 |
+
analyze_btn = gr.Button("🔍 Build Profile & Rewrite", variant="primary")
|
| 318 |
+
rewrite_saved_btn = gr.Button("✍️ Rewrite Using Saved Profile", variant="secondary")
|
| 319 |
|
| 320 |
with gr.Column(scale=1):
|
| 321 |
+
profile_md = gr.Markdown(
|
| 322 |
+
label="🧬 Style Profile",
|
| 323 |
+
value="",
|
| 324 |
+
)
|
| 325 |
rewritten_output = gr.Textbox(
|
| 326 |
label="✨ Rewritten Text",
|
| 327 |
lines=12,
|
|
|
|
| 342 |
inputs=[abstracts_input, original_text_input],
|
| 343 |
outputs=[rewritten_output, metrics_output]
|
| 344 |
)
|
| 345 |
+
|
| 346 |
+
analyze_only_btn.click(
|
| 347 |
+
fn=analyze_style_only,
|
| 348 |
+
inputs=[abstracts_input],
|
| 349 |
+
outputs=[profile_md, metrics_output, style_profile_state]
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
rewrite_saved_btn.click(
|
| 353 |
+
fn=rewrite_with_profile,
|
| 354 |
+
inputs=[original_text_input, style_profile_state],
|
| 355 |
+
outputs=[rewritten_output, metrics_output]
|
| 356 |
+
)
|
| 357 |
|
| 358 |
# Add examples
|
| 359 |
gr.Examples(
|