File size: 1,037 Bytes
991fe7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()