Babajaan commited on
Commit
f66e2c9
·
verified ·
1 Parent(s): 7a7db9f

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +111 -12
  2. app.py +234 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,12 +1,111 @@
1
- ---
2
- title: Writing Style
3
- emoji: 🔥
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 5.47.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🔬 StyleForge Lite
2
+
3
+ A minimal Gradio app that learns your scientific writing style from pasted abstracts and rewrites new text in that style using the Groq API.
4
+
5
+ ## Features
6
+
7
+ - **Style Learning**: Paste 3-8 abstracts to build your writing profile
8
+ - **Text Rewriting**: Rewrite any text in your learned scientific style
9
+ - **Style Metrics**: Get detailed scoring on:
10
+ - Average sentence length
11
+ - Passive voice ratio
12
+ - Vocabulary match (% scientific verbs)
13
+ - Flow markers (context→method→result→implication)
14
+ - Total score (/20)
15
+ - **Deployment Guide**: Built-in instructions for Hugging Face Spaces
16
+
17
+ ## Quick Deploy to Hugging Face Spaces
18
+
19
+ ### Method 1: Direct Clone (Recommended)
20
+
21
+ 1. **Clone this repository**:
22
+ ```bash
23
+ git clone https://huggingface.co/spaces/Babajaan/Writing-Style
24
+ cd Writing-Style
25
+ ```
26
+
27
+ 2. **Set your API key**:
28
+ - Go to your Space settings
29
+ - Add `GROQ_API_KEY` as a secret with your Groq API key
30
+ - Get your API key from [Groq Console](https://console.groq.com)
31
+
32
+ 3. **Deploy**: The Space will automatically rebuild with your changes
33
+
34
+ ### Method 2: Create New Space
35
+
36
+ 1. **Create new Space**:
37
+ - Go to [Hugging Face Spaces](https://huggingface.co/spaces)
38
+ - Click "Create new Space"
39
+ - Choose "Gradio" as SDK
40
+ - Set visibility (Public/Private)
41
+
42
+ 2. **Upload files**:
43
+ - Upload `app.py`
44
+ - Upload `requirements.txt`
45
+ - Upload this `README.md`
46
+
47
+ 3. **Configure environment**:
48
+ - Add `GROQ_API_KEY` in Space settings → Secrets
49
+ - Set Space hardware (CPU is sufficient)
50
+
51
+ 4. **Deploy**: The Space will automatically build and deploy
52
+
53
+ ## Environment Variables
54
+
55
+ - `GROQ_API_KEY`: Your Groq API key (required)
56
+
57
+ ## Requirements
58
+
59
+ - Python 3.8+
60
+ - Gradio >= 4.44.0
61
+ - Groq >= 0.9.0
62
+
63
+ ## Usage
64
+
65
+ 1. **Paste Abstracts**: Input 3-8 of your scientific abstracts in the first text box
66
+ 2. **Enter Text**: Input the text you want to rewrite in the second text box
67
+ 3. **Click "Build Profile & Rewrite"**: The app will analyze your style and rewrite the text
68
+ 4. **View Results**: See the rewritten text and style metrics
69
+
70
+ ## Style Analysis
71
+
72
+ The app analyzes your writing style based on:
73
+
74
+ - **Sentence Length**: Optimal 15-25 words per sentence
75
+ - **Voice**: Preference for active over passive voice
76
+ - **Vocabulary**: Use of precise scientific verbs (identify, demonstrate, suggest, etc.)
77
+ - **Flow**: Logical progression from context → method → result → implication
78
+
79
+ ## Troubleshooting
80
+
81
+ - **Build fails**: Check `requirements.txt` syntax
82
+ - **API errors**: Verify `GROQ_API_KEY` is set correctly in Space secrets
83
+ - **Import errors**: Ensure all dependencies are in `requirements.txt`
84
+ - **Empty responses**: Check your Groq API key and quota
85
+
86
+ ## Cost Considerations
87
+
88
+ - Groq API has generous free tier
89
+ - Monitor usage in [Groq Console](https://console.groq.com)
90
+ - Consider rate limiting for production use
91
+
92
+ ## Example
93
+
94
+ **Input Abstracts**:
95
+ ```
96
+ Resveratrol (RESL), a natural polyphenol, has been studied for its cancer chemopreventive properties. In this study, a diacetate derivative (RESL43) was synthesized, exhibiting potent cytotoxic and pro-apoptotic effects in U937 cells. Molecular docking indicated that RESL43 may inhibit NFκB by disrupting DNA–protein interactions. These findings support stronger activity than RESL and warrant further investigation.
97
+ ```
98
+
99
+ **Original Text**:
100
+ ```
101
+ Our study shows that the new drug works well. We tested it on cells and found good results. The drug might help treat cancer.
102
+ ```
103
+
104
+ **Rewritten Output**:
105
+ ```
106
+ Our investigation demonstrates that the novel therapeutic compound exhibits significant efficacy in cellular models. Through comprehensive in vitro analysis, we identified substantial cytotoxic activity and pro-apoptotic effects in U937 cell lines. These findings suggest the potential utility of this compound as a chemopreventive agent and warrant further investigation into its molecular mechanisms.
107
+ ```
108
+
109
+ ## License
110
+
111
+ This project is open source and available under the MIT License.
app.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import json
4
+ import re
5
+ from groq import Groq
6
+ from typing import List, Dict, Tuple
7
+
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)
14
+ sentences = [s.strip() for s in sentences if s.strip()]
15
+
16
+ if not sentences:
17
+ return {
18
+ "avg_sentence_length": 0,
19
+ "passive_voice_ratio": 0,
20
+ "vocabulary_match": 0,
21
+ "flow_markers": 0,
22
+ "total_score": 0
23
+ }
24
+
25
+ # Average sentence length
26
+ avg_length = sum(len(s.split()) for s in sentences) / len(sentences)
27
+
28
+ # Passive voice detection (simplified)
29
+ passive_indicators = ['was', 'were', 'been', 'being', 'is', 'are', 'am']
30
+ passive_count = sum(1 for sentence in sentences
31
+ for word in passive_indicators
32
+ if word in sentence.lower().split())
33
+ passive_ratio = min(passive_count / len(sentences), 1.0)
34
+
35
+ # Vocabulary match (scientific verbs)
36
+ scientific_verbs = ['identify', 'demonstrate', 'predict', 'suggest', 'support',
37
+ 'indicate', 'reveal', 'establish', 'confirm', 'validate',
38
+ 'analyze', 'examine', 'investigate', 'evaluate', 'assess']
39
+ text_lower = text.lower()
40
+ verb_count = sum(1 for verb in scientific_verbs if verb in text_lower)
41
+ vocab_match = min(verb_count / len(sentences) * 5, 1.0) # Normalize to 0-1
42
+
43
+ # Flow markers (context→method→result→implication)
44
+ flow_words = ['context', 'background', 'method', 'approach', 'result', 'finding',
45
+ 'implication', 'significance', 'conclusion', 'study', 'research',
46
+ 'analysis', 'investigation', 'examination']
47
+ flow_count = sum(1 for word in flow_words if word in text_lower)
48
+ flow_markers = min(flow_count / len(sentences) * 3, 1.0) # Normalize to 0-1
49
+
50
+ # Scoring (out of 20)
51
+ length_score = min(avg_length / 20, 1.0) * 5 # 5 points for ideal length
52
+ passive_score = (1 - passive_ratio) * 5 # 5 points for active voice
53
+ vocab_score = vocab_match * 5 # 5 points for scientific vocabulary
54
+ flow_score = flow_markers * 5 # 5 points for good flow
55
+
56
+ total_score = length_score + passive_score + vocab_score + flow_score
57
+
58
+ return {
59
+ "avg_sentence_length": round(avg_length, 1),
60
+ "passive_voice_ratio": round(passive_ratio * 100, 1),
61
+ "vocabulary_match": round(vocab_match * 100, 1),
62
+ "flow_markers": round(flow_markers * 100, 1),
63
+ "total_score": round(total_score, 1)
64
+ }
65
+
66
+ def build_style_profile_and_rewrite(abstracts: str, original_text: str) -> Tuple[str, str]:
67
+ """Build style profile from abstracts and rewrite original text"""
68
+
69
+ if not abstracts.strip() or not original_text.strip():
70
+ return "Please provide both abstracts and original text.", "{}"
71
+
72
+ try:
73
+ # Create style analysis prompt
74
+ style_prompt = f"""
75
+ You are a scientific writing assistant designed to emulate the style of Dr. Babajan Banaganapalli.
76
+
77
+ STYLE RULES:
78
+ - Employ a formal, confident scientific tone throughout
79
+ - Construct sentences of medium to extended length (approximately 15–25 words)
80
+ - Ensure logical progression: context → methodology → results → implications
81
+ - Favor precise verbs such as identify, demonstrate, predict, suggest, and support
82
+ - Integrate technical terms naturally, minimizing unnecessary jargon
83
+ - Interpret outcomes cautiously, using qualifiers such as "suggest," "support," and "warrant further study"
84
+ - Use active voice predominantly, reserving passive constructions for emphasis
85
+ - Maintain compact paragraphs, each comprising 1–3 sentences
86
+
87
+ Analyze the following abstracts to understand the writing style:
88
+
89
+ {abstracts}
90
+
91
+ Now rewrite the following text in this exact style, preserving all factual information and numerical data:
92
+
93
+ {original_text}
94
+
95
+ Rewrite:
96
+ """
97
+
98
+ # Get completion from Groq
99
+ completion = client.chat.completions.create(
100
+ model="openai/gpt-oss-120b",
101
+ messages=[{"role": "user", "content": style_prompt}],
102
+ temperature=0.7,
103
+ max_completion_tokens=2048,
104
+ top_p=1,
105
+ reasoning_effort="medium"
106
+ )
107
+
108
+ rewritten_text = completion.choices[0].message.content
109
+
110
+ # Analyze the rewritten text for scoring
111
+ metrics = analyze_writing_style(rewritten_text)
112
+
113
+ return rewritten_text, json.dumps(metrics, indent=2)
114
+
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 """
121
+ # How to Deploy StyleForge Lite on Hugging Face Spaces
122
+
123
+ ## Quick Deploy Steps
124
+
125
+ 1. **Fork this repository** on Hugging Face Spaces
126
+ 2. **Set environment variables**:
127
+ - Go to Settings → Secrets
128
+ - Add `GROQ_API_KEY` with your Groq API key
129
+ 3. **Deploy**: The app will automatically build and deploy
130
+
131
+ ## Manual Setup
132
+
133
+ 1. **Create new Space**:
134
+ - Go to [Hugging Face Spaces](https://huggingface.co/spaces)
135
+ - Click "Create new Space"
136
+ - Choose "Gradio" as SDK
137
+ - Set visibility (Public/Private)
138
+
139
+ 2. **Upload files**:
140
+ - Upload `app.py`
141
+ - Upload `requirements.txt`
142
+ - Upload `README.md`
143
+
144
+ 3. **Configure environment**:
145
+ - Add `GROQ_API_KEY` in Space settings
146
+ - Set Space hardware (CPU is sufficient)
147
+
148
+ 4. **Deploy**:
149
+ - The Space will automatically build
150
+ - Wait for deployment to complete
151
+ - Your app will be live at `https://huggingface.co/spaces/your-username/your-space-name`
152
+
153
+ ## Requirements
154
+
155
+ - Groq API key (get from [Groq Console](https://console.groq.com))
156
+ - Hugging Face account
157
+ - Basic understanding of environment variables
158
+
159
+ ## Troubleshooting
160
+
161
+ - **Build fails**: Check `requirements.txt` syntax
162
+ - **API errors**: Verify `GROQ_API_KEY` is set correctly
163
+ - **Import errors**: Ensure all dependencies are in `requirements.txt`
164
+
165
+ ## Cost Considerations
166
+
167
+ - Groq API has generous free tier
168
+ - Monitor usage in Groq Console
169
+ - Consider rate limiting for production use
170
+ """
171
+
172
+ # Create Gradio interface
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():
179
+ with gr.Column(scale=1):
180
+ abstracts_input = gr.Textbox(
181
+ label="📝 Paste 3-8 Abstracts (Your Writing Samples)",
182
+ placeholder="Paste your scientific abstracts here to build a style profile...",
183
+ lines=10,
184
+ max_lines=15
185
+ )
186
+
187
+ original_text_input = gr.Textbox(
188
+ label="📄 Original Text to Rewrite",
189
+ placeholder="Enter the text you want to rewrite in your style...",
190
+ lines=8,
191
+ max_lines=12
192
+ )
193
+
194
+ analyze_btn = gr.Button("🔍 Build Profile & Rewrite", variant="primary", size="lg")
195
+
196
+ with gr.Column(scale=1):
197
+ rewritten_output = gr.Textbox(
198
+ label="✨ Rewritten Text",
199
+ lines=12,
200
+ max_lines=15,
201
+ interactive=False
202
+ )
203
+
204
+ metrics_output = gr.JSON(
205
+ label="📊 Style Metrics (Score out of 20)",
206
+ interactive=False
207
+ )
208
+
209
+ with gr.Tab("📋 How to Deploy"):
210
+ deployment_instructions = gr.Markdown(create_deployment_instructions())
211
+
212
+ # Connect the button to the function
213
+ analyze_btn.click(
214
+ fn=build_style_profile_and_rewrite,
215
+ inputs=[abstracts_input, original_text_input],
216
+ outputs=[rewritten_output, metrics_output]
217
+ )
218
+
219
+ # Add examples
220
+ gr.Examples(
221
+ examples=[
222
+ [
223
+ """Resveratrol (RESL), a natural polyphenol, has been studied for its cancer chemopreventive properties. In this study, a diacetate derivative (RESL43) was synthesized, exhibiting potent cytotoxic and pro-apoptotic effects in U937 cells. Molecular docking indicated that RESL43 may inhibit NFκB by disrupting DNA–protein interactions. These findings support stronger activity than RESL and warrant further investigation.
224
+
225
+ Tuberculosis persists as a major infectious disease globally. Here, we developed the first three-dimensional structural model of Mtb-MurA using homology modeling and molecular dynamics. Docking studies demonstrated that 5-sulfonoxyanthranilic acid derivatives exhibited optimal interaction with Mtb-MurA. This supports their potential for the rational design of selective enzyme inhibitors.""",
226
+ "Our study shows that the new drug works well. We tested it on cells and found good results. The drug might help treat cancer."
227
+ ]
228
+ ],
229
+ inputs=[abstracts_input, original_text_input],
230
+ label="💡 Example"
231
+ )
232
+
233
+ if __name__ == "__main__":
234
+ app.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=4.44.0
2
+ groq>=0.9.0