Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
API_KEY = os.getenv("API_KEY")
|
| 6 |
+
|
| 7 |
+
sentiment_model = pipeline(
|
| 8 |
+
"sentiment-analysis",
|
| 9 |
+
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
|
| 10 |
+
device=0
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def analyze_sentiment(text, api_key):
|
| 14 |
+
if api_key != API_KEY:
|
| 15 |
+
return {"error": "Invalid API Key"}
|
| 16 |
+
return sentiment_model(text)
|
| 17 |
+
|
| 18 |
+
def hello_world():
|
| 19 |
+
return {"message": "Hello World"}
|
| 20 |
+
|
| 21 |
+
# Делаем два интерфейса
|
| 22 |
+
analyze_app = gr.Interface(
|
| 23 |
+
fn=analyze_sentiment,
|
| 24 |
+
inputs=[gr.Textbox(label="Text"), gr.Textbox(label="API Key", type="password")],
|
| 25 |
+
outputs="json",
|
| 26 |
+
title="Sentiment Analysis"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
hello_app = gr.Interface(
|
| 30 |
+
fn=hello_world,
|
| 31 |
+
inputs=[],
|
| 32 |
+
outputs="json",
|
| 33 |
+
title="Hello Endpoint"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Собираем в один Gradio Blocks
|
| 37 |
+
demo = gr.Blocks()
|
| 38 |
+
|
| 39 |
+
with demo:
|
| 40 |
+
gr.Markdown("# Multi-endpoint replacement for FastAPI")
|
| 41 |
+
with gr.Tab("Analyze"):
|
| 42 |
+
analyze_app.render()
|
| 43 |
+
with gr.Tab("Hello"):
|
| 44 |
+
hello_app.render()
|
| 45 |
+
|
| 46 |
+
demo.launch()
|