get_sentiment / app.py
SmartDataAlex's picture
Create app.py
991fe7c verified
Raw History Blame Contribute Delete
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()