Spaces:
Paused
Paused
Download app.py from SmartDataAlex/get_sentiment: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/SmartDataAlex/get_sentiment/resolve/main/app.py
- Command line
-
hf download hf://spaces/SmartDataAlex/get_sentiment/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SmartDataAlex/get_sentiment/resolve/main/app.py
1.04 kB
| import os | |
| import gradio as gr | |
| from transformers import pipeline | |
| API_KEY = os.getenv("API_KEY") | |
| sentiment_model = pipeline( | |
| "sentiment-analysis", | |
| model="cardiffnlp/twitter-roberta-base-sentiment-latest", | |
| device=0 | |
| ) | |
| def analyze_sentiment(text, api_key): | |
| if api_key != API_KEY: | |
| return {"error": "Invalid API Key"} | |
| return sentiment_model(text) | |
| def hello_world(): | |
| return {"message": "Hello World"} | |
| # Делаем два интерфейса | |
| analyze_app = gr.Interface( | |
| fn=analyze_sentiment, | |
| inputs=[gr.Textbox(label="Text"), gr.Textbox(label="API Key", type="password")], | |
| outputs="json", | |
| title="Sentiment Analysis" | |
| ) | |
| hello_app = gr.Interface( | |
| fn=hello_world, | |
| inputs=[], | |
| outputs="json", | |
| title="Hello Endpoint" | |
| ) | |
| # Собираем в один Gradio Blocks | |
| demo = gr.Blocks() | |
| with demo: | |
| gr.Markdown("# Multi-endpoint replacement for FastAPI") | |
| with gr.Tab("Analyze"): | |
| analyze_app.render() | |
| with gr.Tab("Hello"): | |
| hello_app.render() | |
| demo.launch() |