Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import gradio as gr
|
|
3 |
import speech_recognition as sr
|
4 |
import time
|
5 |
import difflib
|
6 |
-
import
|
7 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
from happytransformer import HappyTextToText, TTSettings
|
9 |
|
@@ -17,7 +17,6 @@ CSS = """
|
|
17 |
.diff-del { color: #ef4444; background: #fee2e2; padding: 2px 4px; border-radius: 4px; }
|
18 |
"""
|
19 |
|
20 |
-
# Three.js Template
|
21 |
THREEJS_TEMPLATE = """
|
22 |
<div id="container"></div>
|
23 |
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
|
@@ -100,7 +99,6 @@ window.updateGrammarVisuals = (score) => {
|
|
100 |
};
|
101 |
</script>
|
102 |
"""
|
103 |
-
|
104 |
# Load models
|
105 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
106 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
@@ -136,25 +134,40 @@ def analyze_grammar(text):
|
|
136 |
}
|
137 |
|
138 |
def process_input(audio_path, text):
|
139 |
-
|
140 |
-
|
141 |
-
with sr.AudioFile(audio_path) as source:
|
142 |
-
audio = recognizer.record(source)
|
143 |
try:
|
|
|
|
|
|
|
144 |
text = recognizer.recognize_google(audio)
|
145 |
-
except
|
146 |
-
return
|
|
|
|
|
|
|
|
|
|
|
147 |
|
|
|
148 |
if not text.strip():
|
149 |
-
return "No input", 0, "", ""
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
with gr.Blocks(css=CSS) as app:
|
160 |
gr.Markdown("""
|
@@ -182,17 +195,18 @@ with gr.Blocks(css=CSS) as app:
|
|
182 |
diff_output = gr.HTML(label="π Text Corrections")
|
183 |
hidden_trigger = gr.HTML(visible=False)
|
184 |
|
|
|
185 |
gr.Markdown("### Example Sentences")
|
186 |
gr.Examples(
|
187 |
examples=[
|
188 |
-
["I is going to the park yesterday."],
|
189 |
["Their happy about there test results."],
|
190 |
["She dont like apples, but she like bananas."]
|
191 |
],
|
192 |
-
inputs=[text_input],
|
193 |
outputs=[text_input, grammar_score, diff_output, hidden_trigger],
|
194 |
-
fn=process_input,
|
195 |
-
cache_examples=
|
196 |
)
|
197 |
|
198 |
submit_btn.click(
|
@@ -211,6 +225,5 @@ if __name__ == "__main__":
|
|
211 |
app.launch(
|
212 |
server_name="0.0.0.0",
|
213 |
server_port=7860,
|
214 |
-
share=
|
215 |
-
favicon_path="https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png"
|
216 |
)
|
|
|
3 |
import speech_recognition as sr
|
4 |
import time
|
5 |
import difflib
|
6 |
+
import os
|
7 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
from happytransformer import HappyTextToText, TTSettings
|
9 |
|
|
|
17 |
.diff-del { color: #ef4444; background: #fee2e2; padding: 2px 4px; border-radius: 4px; }
|
18 |
"""
|
19 |
|
|
|
20 |
THREEJS_TEMPLATE = """
|
21 |
<div id="container"></div>
|
22 |
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
|
|
|
99 |
};
|
100 |
</script>
|
101 |
"""
|
|
|
102 |
# Load models
|
103 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
104 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
|
134 |
}
|
135 |
|
136 |
def process_input(audio_path, text):
|
137 |
+
# Handle audio input
|
138 |
+
if audio_path and os.path.exists(audio_path):
|
|
|
|
|
139 |
try:
|
140 |
+
recognizer = sr.Recognizer()
|
141 |
+
with sr.AudioFile(audio_path) as source:
|
142 |
+
audio = recognizer.record(source)
|
143 |
text = recognizer.recognize_google(audio)
|
144 |
+
except Exception as e:
|
145 |
+
return [
|
146 |
+
"Audio processing error",
|
147 |
+
0,
|
148 |
+
f"<span style='color:red'>Error: {str(e)}</span>",
|
149 |
+
"<script>window.updateGrammarVisuals(0)</script>"
|
150 |
+
]
|
151 |
|
152 |
+
# Handle text input
|
153 |
if not text.strip():
|
154 |
+
return ["No input provided", 0, "", "<script>window.updateGrammarVisuals(0)</script>"]
|
155 |
|
156 |
+
try:
|
157 |
+
results = analyze_grammar(text)
|
158 |
+
return [
|
159 |
+
results['original'],
|
160 |
+
results['score'],
|
161 |
+
results['diff_html'],
|
162 |
+
f"<script>window.updateGrammarVisuals({results['score']})</script>"
|
163 |
+
]
|
164 |
+
except Exception as e:
|
165 |
+
return [
|
166 |
+
"Analysis error",
|
167 |
+
0,
|
168 |
+
f"<span style='color:red'>Error: {str(e)}</span>",
|
169 |
+
"<script>window.updateGrammarVisuals(0)</script>"
|
170 |
+
]
|
171 |
|
172 |
with gr.Blocks(css=CSS) as app:
|
173 |
gr.Markdown("""
|
|
|
195 |
diff_output = gr.HTML(label="π Text Corrections")
|
196 |
hidden_trigger = gr.HTML(visible=False)
|
197 |
|
198 |
+
# Fixed examples configuration
|
199 |
gr.Markdown("### Example Sentences")
|
200 |
gr.Examples(
|
201 |
examples=[
|
202 |
+
["I is going to the park yesterday."], # Text-only examples
|
203 |
["Their happy about there test results."],
|
204 |
["She dont like apples, but she like bananas."]
|
205 |
],
|
206 |
+
inputs=[text_input], # Only text input
|
207 |
outputs=[text_input, grammar_score, diff_output, hidden_trigger],
|
208 |
+
fn=lambda text: process_input(None, text), # Explicitly handle text-only examples
|
209 |
+
cache_examples=False # Disable caching to prevent startup issues
|
210 |
)
|
211 |
|
212 |
submit_btn.click(
|
|
|
225 |
app.launch(
|
226 |
server_name="0.0.0.0",
|
227 |
server_port=7860,
|
228 |
+
share=False # Disable sharing until basic functionality works
|
|
|
229 |
)
|