File size: 2,824 Bytes
e555347
4c897b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73914ef
9038e16
 
 
73914ef
 
935fd9c
e2ed7a3
935fd9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73914ef
4c897b1
e555347
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Model Demo</title>

    <script type="module"
        src="https://gradio-lite-previews.s3.amazonaws.com/PINNED_HF_HUB/dist/lite.js">
    </script>

    <link rel="stylesheet"
        href="https://gradio-lite-previews.s3.amazonaws.com/PINNED_HF_HUB/dist/lite.css">
</head>
<body>
    <gradio-lite>
    <gradio-requirements>
typing-extensions==4.15.0
transformers-js-py
</gradio-requirements>

    <gradio-file name="app.py" entrypoint>
       import gradio as gr
from transformers_js import import_transformers_js

# Load Transformers.js
transformers = await import_transformers_js()
pipeline = transformers.pipeline

# Model 1: Sentiment Analysis
sentiment_model = await pipeline(
    "sentiment-analysis",
    "Xenova/distilbert-base-uncased-finetuned-sst-2-english"
)

# Model 2: Text Generation
text_model = await pipeline(
    "text-generation",
    "Xenova/gpt2"
)


# Callback 1: Sentiment Analysis
async def analyze_sentiment(text):
    if not text or not text.strip():
        return "Please enter some text."

    result = await sentiment_model(text)
    result = result[0]

    return (
        f"Sentiment: {result['label']}\n"
        f"Confidence: {result['score']:.2%}"
    )


# Callback 2: Text Generation
async def generate_text(prompt):
    if not prompt or not prompt.strip():
        return "Please enter a prompt."

    result = await text_model(
        prompt,
        max_new_tokens=40,
        do_sample=True,
        temperature=0.7
    )

    return result[0]["generated_text"]


# Gradio GUI
with gr.Blocks(title="AI Model Demo") as demo:
    gr.Markdown("# AI Model Demo")
    gr.Markdown(
        "An application using two pretrained models."
    )

    with gr.Tab("Sentiment Analysis"):
        sentiment_input = gr.Textbox(
            label="Enter a sentence",
            placeholder="I love learning artificial intelligence!"
        )

        sentiment_button = gr.Button("Analyze Sentiment")
        sentiment_output = gr.Textbox(label="Sentiment Result")

        sentiment_button.click(
            fn=analyze_sentiment,
            inputs=sentiment_input,
            outputs=sentiment_output
        )

    with gr.Tab("Text Generation"):
        prompt_input = gr.Textbox(
            label="Enter a prompt",
            placeholder="Artificial intelligence is"
        )

        generate_button = gr.Button("Generate Text")
        generated_output = gr.Textbox(
            label="Generated Text",
            lines=5
        )

        generate_button.click(
            fn=generate_text,
            inputs=prompt_input,
            outputs=generated_output
        )

demo.launch()
    </gradio-file>
</body>
</html>