Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,31 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import spacy
|
| 4 |
from textblob import TextBlob
|
| 5 |
-
import json
|
| 6 |
import requests
|
| 7 |
from gradio_client import Client
|
| 8 |
|
| 9 |
# Initialize models
|
| 10 |
-
nlp = spacy.load("
|
| 11 |
spell_checker = pipeline("text2text-generation", model="oliverguhr/spelling-correction-english-base")
|
| 12 |
|
| 13 |
-
def preprocess_and_forward(text: str) ->
|
| 14 |
"""
|
| 15 |
Processes the input text and forwards it to the Gradio client for space_17.
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Instantiate the Gradio Client for space_17
|
| 21 |
-
client = Client("Frenchizer/space_17")
|
| 22 |
-
|
| 23 |
-
# Forward preprocessed text to the Gradio client for space_17
|
| 24 |
-
try:
|
| 25 |
-
context_response = client.predict(processed_text)
|
| 26 |
-
preprocessing_results["context_response"] = context_response
|
| 27 |
-
except Exception as e:
|
| 28 |
-
preprocessing_results["context_error"] = str(e)
|
| 29 |
-
|
| 30 |
-
return preprocessing_results
|
| 31 |
|
| 32 |
def preprocess_text(text: str):
|
|
|
|
|
|
|
|
|
|
| 33 |
result = {
|
| 34 |
"spell_suggestions": [],
|
| 35 |
"entities": [],
|
| 36 |
"tags": []
|
| 37 |
}
|
| 38 |
-
|
| 39 |
# Basic spell checking using TextBlob
|
| 40 |
corrected_text = str(TextBlob(text).correct())
|
| 41 |
if corrected_text != text:
|
|
@@ -59,14 +50,28 @@ def preprocess_text(text: str):
|
|
| 59 |
# Extract potential tags (hashtags, mentions, etc.)
|
| 60 |
result["tags"] = [token.text for token in doc if token.text.startswith(('#', '@'))]
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Gradio interface
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
input_text = gr.Textbox(label="Input Text")
|
| 67 |
-
|
| 68 |
preprocess_button = gr.Button("Process")
|
| 69 |
-
preprocess_button.click(fn=preprocess_and_forward, inputs=[input_text], outputs=[
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch()
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
import spacy
|
| 4 |
from textblob import TextBlob
|
|
|
|
| 5 |
import requests
|
| 6 |
from gradio_client import Client
|
| 7 |
|
| 8 |
# Initialize models
|
| 9 |
+
nlp = spacy.load("en_core_web_trf") # More accurate NER
|
| 10 |
spell_checker = pipeline("text2text-generation", model="oliverguhr/spelling-correction-english-base")
|
| 11 |
|
| 12 |
+
def preprocess_and_forward(text: str) -> str:
|
| 13 |
"""
|
| 14 |
Processes the input text and forwards it to the Gradio client for space_17.
|
| 15 |
+
Returns only the final translated text.
|
| 16 |
"""
|
| 17 |
+
processed_text, _ = preprocess_text(text)
|
| 18 |
+
return forward_to_translation(processed_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def preprocess_text(text: str):
|
| 21 |
+
"""
|
| 22 |
+
Applies spell-checking and named entity recognition (NER) to preprocess text.
|
| 23 |
+
"""
|
| 24 |
result = {
|
| 25 |
"spell_suggestions": [],
|
| 26 |
"entities": [],
|
| 27 |
"tags": []
|
| 28 |
}
|
| 29 |
+
|
| 30 |
# Basic spell checking using TextBlob
|
| 31 |
corrected_text = str(TextBlob(text).correct())
|
| 32 |
if corrected_text != text:
|
|
|
|
| 50 |
# Extract potential tags (hashtags, mentions, etc.)
|
| 51 |
result["tags"] = [token.text for token in doc if token.text.startswith(('#', '@'))]
|
| 52 |
|
| 53 |
+
# Choose the best-corrected version
|
| 54 |
+
final_text = spell_checked if spell_checked != text else corrected_text
|
| 55 |
+
|
| 56 |
+
return final_text, result
|
| 57 |
+
|
| 58 |
+
def forward_to_translation(text: str) -> str:
|
| 59 |
+
"""
|
| 60 |
+
Sends preprocessed text to space_17 for translation and returns only the translated text.
|
| 61 |
+
"""
|
| 62 |
+
client = Client("Frenchizer/space_17")
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
return client.predict(text)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error: {str(e)}"
|
| 68 |
|
| 69 |
# Gradio interface
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
input_text = gr.Textbox(label="Input Text")
|
| 72 |
+
output_text = gr.Textbox(label="Output Text") # Returns only text
|
| 73 |
preprocess_button = gr.Button("Process")
|
| 74 |
+
preprocess_button.click(fn=preprocess_and_forward, inputs=[input_text], outputs=[output_text])
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.launch()
|