Download app.py from OneManLab/space_20: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/OneManLab/space_20/resolve/04e2cd0a5bd05cf0eb64e8f21e06bf3ec0de8521/app.py
- Command line
-
hf download hf://spaces/OneManLab/space_20@04e2cd0a5bd05cf0eb64e8f21e06bf3ec0de8521/app.py
-
curl -L -o app.py https://huggingface.co/spaces/OneManLab/space_20/resolve/04e2cd0a5bd05cf0eb64e8f21e06bf3ec0de8521/app.py
2.34 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import spacy | |
| from textblob import TextBlob | |
| import json | |
| import requests | |
| from gradio_client import Client | |
| # Initialize models | |
| nlp = spacy.load("en_core_web_sm") # Use "en_core_web_trf" if more accuracy is needed | |
| spell_checker = pipeline("text2text-generation", model="oliverguhr/spelling-correction-english-base") | |
| def preprocess_and_forward(text: str) -> dict: | |
| """ | |
| Processes the input text and forwards it to the Gradio client for space_17. | |
| """ | |
| # Preprocess the text | |
| processed_text, preprocessing_results = preprocess_text(text) | |
| # Instantiate the Gradio Client for space_17 | |
| client = Client("Frenchizer/space_17") | |
| # Forward preprocessed text to the Gradio client for space_17 | |
| try: | |
| context_response = client.predict(processed_text) | |
| preprocessing_results["context_response"] = context_response | |
| except Exception as e: | |
| preprocessing_results["context_error"] = str(e) | |
| return preprocessing_results | |
| def preprocess_text(text: str): | |
| result = { | |
| "spell_suggestions": [], | |
| "entities": [], | |
| "tags": [] | |
| } | |
| # Basic spell checking using TextBlob | |
| corrected_text = str(TextBlob(text).correct()) | |
| if corrected_text != text: | |
| result["spell_suggestions"].append({ | |
| "original": text, | |
| "corrected": corrected_text | |
| }) | |
| # Transformer-based spell check | |
| spell_checked = spell_checker(text, max_length=512)[0]['generated_text'] | |
| if spell_checked != text and spell_checked != corrected_text: | |
| result["spell_suggestions"].append({ | |
| "original": text, | |
| "corrected": spell_checked | |
| }) | |
| # NER with spaCy | |
| doc = nlp(text) | |
| result["entities"] = [{"text": ent.text, "label": ent.label_} for ent in doc.ents] | |
| # Extract potential tags (hashtags, mentions, etc.) | |
| result["tags"] = [token.text for token in doc if token.text.startswith(('#', '@'))] | |
| return text | |
| # Gradio interface | |
| with gr.Blocks() as demo: | |
| input_text = gr.Textbox(label="Input Text") | |
| output_json = gr.JSON(label="Processing Results") | |
| preprocess_button = gr.Button("Process") | |
| preprocess_button.click(fn=preprocess_and_forward, inputs=[input_text], outputs=[output_json]) | |
| if __name__ == "__main__": | |
| demo.launch() | |