Spaces:
Sleeping
Sleeping
qiankun
Refactor app to implement multilingual sentiment analysis using Hugging Face's pipeline, replacing the previous prompt enhancement functionality. Update Gradio interface and adjust requirements for dependencies.
5e1197b | import gradio as gr | |
| from transformers import pipeline | |
| # η΄ζ₯εΌη¨ Hugging Face Hub δΈη樑ε | |
| MODEL_REPO = "tabularisai/multilingual-sentiment-analysis" | |
| # εε§εζ ζεζ pipeline | |
| classifier = pipeline("sentiment-analysis", model=MODEL_REPO) | |
| def analyze_sentiment(text): | |
| try: | |
| results = classifier(text) | |
| # ε€ηεζ‘εε€ζ‘θΎε ₯ | |
| if isinstance(results, list): | |
| output_lines = [] | |
| for result in results: | |
| label = result.get('label', '') | |
| score = result.get('score', 0) | |
| output_lines.append(f"Label: {label}, Confidence: {score:.2%}") | |
| return "\n".join(output_lines) | |
| else: | |
| label = results.get('label', '') | |
| score = results.get('score', 0) | |
| return f"Label: {label}, Confidence: {score:.2%}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Gradio Web ηι’ | |
| with gr.Blocks(title="Multilingual Sentiment Analysis") as app: | |
| gr.Markdown("## π Multilingual Sentiment Analysis\nAnalyze sentiment for texts in multiple languages using [tabularisai/multilingual-sentiment-analysis](https://huggingface.co/tabularisai/multilingual-sentiment-analysis).") | |
| prompt_input = gr.Textbox(label="Enter your text here", lines=3, placeholder="Type or paste your sentence...") | |
| output = gr.Textbox(label="Sentiment Result", lines=2) | |
| btn = gr.Button("Analyze Sentiment") | |
| btn.click( | |
| fn=analyze_sentiment, | |
| inputs=prompt_input, | |
| outputs=output | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() | |