Spaces:
Sleeping
Sleeping
Download app.py from aqeelabdullah654/NLP-model: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/aqeelabdullah654/NLP-model/resolve/main/app.py
- Command line
-
hf download hf://spaces/aqeelabdullah654/NLP-model/app.py
-
curl -L -o app.py https://huggingface.co/spaces/aqeelabdullah654/NLP-model/resolve/main/app.py
2.67 kB
| import spaces | |
| import gradio as gr | |
| from transformers import ( | |
| AutoTokenizer, | |
| AutoModelForSequenceClassification, | |
| TextClassificationPipeline, | |
| ) | |
| MODEL_REPO = "Ahmed-AI-Engineer/smartassist-nlp-intent-model" | |
| CONFIDENCE_THRESHOLD = 0.60 | |
| print(f"Loading model from: {MODEL_REPO}") | |
| # ----------------------------------- | |
| # Load tokenizer | |
| # ----------------------------------- | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| MODEL_REPO | |
| ) | |
| # ----------------------------------- | |
| # Load model | |
| # ----------------------------------- | |
| model = AutoModelForSequenceClassification.from_pretrained( | |
| MODEL_REPO | |
| ) | |
| # IMPORTANT: | |
| # ZeroGPU supports CUDA placement during startup | |
| # through its CUDA emulation layer. | |
| model = model.to("cuda") | |
| model.eval() | |
| print("Model loaded successfully.") | |
| # ----------------------------------- | |
| # Create pipeline | |
| # ----------------------------------- | |
| classifier = TextClassificationPipeline( | |
| model=model, | |
| tokenizer=tokenizer, | |
| device=0 | |
| ) | |
| # ----------------------------------- | |
| # Prediction function | |
| # ----------------------------------- | |
| def predict_intent(text: str): | |
| text = text.strip() | |
| if not text: | |
| return { | |
| "intent": "", | |
| "confidence": 0.0, | |
| "needs_confirmation": True, | |
| "raw_text": text, | |
| } | |
| result = classifier(text)[0] | |
| confidence = float(result["score"]) | |
| return { | |
| "intent": result["label"], | |
| "confidence": confidence, | |
| "needs_confirmation": confidence < CONFIDENCE_THRESHOLD, | |
| "raw_text": text, | |
| } | |
| # ----------------------------------- | |
| # Gradio UI | |
| # ----------------------------------- | |
| with gr.Blocks( | |
| title="SmartAssist NLP API" | |
| ) as demo: | |
| gr.Markdown( | |
| """ | |
| # SmartAssist NLP API | |
| Intent classification for SmartAssist home automation commands. | |
| Supports: | |
| - English | |
| - Urdu | |
| - Roman Urdu | |
| """ | |
| ) | |
| text_input = gr.Textbox( | |
| label="Voice command text", | |
| placeholder="Example: light on karo" | |
| ) | |
| predict_button = gr.Button( | |
| "Predict", | |
| variant="primary" | |
| ) | |
| prediction_output = gr.JSON( | |
| label="Prediction" | |
| ) | |
| predict_button.click( | |
| fn=predict_intent, | |
| inputs=text_input, | |
| outputs=prediction_output, | |
| api_name="predict" | |
| ) | |
| text_input.submit( | |
| fn=predict_intent, | |
| inputs=text_input, | |
| outputs=prediction_output, | |
| api_name=False | |
| ) | |
| # ----------------------------------- | |
| # Launch Space | |
| # ----------------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() |