Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,30 @@
|
|
| 1 |
-
```python
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"sentiment-analysis",
|
| 8 |
-
|
| 9 |
)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
text_model = pipeline(
|
| 13 |
"text-generation",
|
| 14 |
-
|
| 15 |
)
|
| 16 |
|
| 17 |
|
| 18 |
-
# Callback 1:
|
| 19 |
-
def analyze_sentiment(text):
|
| 20 |
if not text or not text.strip():
|
| 21 |
return "Please enter some text."
|
| 22 |
|
| 23 |
-
result = sentiment_model(text)
|
|
|
|
| 24 |
|
| 25 |
return (
|
| 26 |
f"Sentiment: {result['label']}\n"
|
|
@@ -28,28 +32,26 @@ def analyze_sentiment(text):
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
| 31 |
-
# Callback 2:
|
| 32 |
-
def generate_text(prompt):
|
| 33 |
if not prompt or not prompt.strip():
|
| 34 |
return "Please enter a prompt."
|
| 35 |
|
| 36 |
-
result = text_model(
|
| 37 |
prompt,
|
| 38 |
max_new_tokens=40,
|
| 39 |
do_sample=True,
|
| 40 |
-
temperature=0.7
|
| 41 |
-
pad_token_id=text_model.tokenizer.eos_token_id
|
| 42 |
)
|
| 43 |
|
| 44 |
return result[0]["generated_text"]
|
| 45 |
|
| 46 |
|
| 47 |
-
# Gradio
|
| 48 |
with gr.Blocks(title="AI Model Demo") as demo:
|
| 49 |
gr.Markdown("# AI Model Demo")
|
| 50 |
gr.Markdown(
|
| 51 |
-
"An application using two pretrained "
|
| 52 |
-
"Hugging Face Transformer models."
|
| 53 |
)
|
| 54 |
|
| 55 |
with gr.Tab("Sentiment Analysis"):
|
|
@@ -57,6 +59,7 @@ with gr.Blocks(title="AI Model Demo") as demo:
|
|
| 57 |
label="Enter a sentence",
|
| 58 |
placeholder="I love learning artificial intelligence!"
|
| 59 |
)
|
|
|
|
| 60 |
sentiment_button = gr.Button("Analyze Sentiment")
|
| 61 |
sentiment_output = gr.Textbox(label="Sentiment Result")
|
| 62 |
|
|
@@ -71,6 +74,7 @@ with gr.Blocks(title="AI Model Demo") as demo:
|
|
| 71 |
label="Enter a prompt",
|
| 72 |
placeholder="Artificial intelligence is"
|
| 73 |
)
|
|
|
|
| 74 |
generate_button = gr.Button("Generate Text")
|
| 75 |
generated_output = gr.Textbox(
|
| 76 |
label="Generated Text",
|
|
@@ -83,7 +87,4 @@ with gr.Blocks(title="AI Model Demo") as demo:
|
|
| 83 |
outputs=generated_output
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
demo.launch()
|
| 89 |
-
```
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers_js_py import import_transformers_js
|
| 3 |
|
| 4 |
+
# Load Transformers.js
|
| 5 |
+
transformers = await import_transformers_js()
|
| 6 |
+
pipeline = transformers.pipeline
|
| 7 |
+
|
| 8 |
+
# Model 1: Sentiment Analysis
|
| 9 |
+
sentiment_model = await pipeline(
|
| 10 |
"sentiment-analysis",
|
| 11 |
+
"Xenova/distilbert-base-uncased-finetuned-sst-2-english"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Model 2: Text Generation
|
| 15 |
+
text_model = await pipeline(
|
| 16 |
"text-generation",
|
| 17 |
+
"Xenova/gpt2"
|
| 18 |
)
|
| 19 |
|
| 20 |
|
| 21 |
+
# Callback 1: Sentiment Analysis
|
| 22 |
+
async def analyze_sentiment(text):
|
| 23 |
if not text or not text.strip():
|
| 24 |
return "Please enter some text."
|
| 25 |
|
| 26 |
+
result = await sentiment_model(text)
|
| 27 |
+
result = result[0]
|
| 28 |
|
| 29 |
return (
|
| 30 |
f"Sentiment: {result['label']}\n"
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
|
| 35 |
+
# Callback 2: Text Generation
|
| 36 |
+
async def generate_text(prompt):
|
| 37 |
if not prompt or not prompt.strip():
|
| 38 |
return "Please enter a prompt."
|
| 39 |
|
| 40 |
+
result = await text_model(
|
| 41 |
prompt,
|
| 42 |
max_new_tokens=40,
|
| 43 |
do_sample=True,
|
| 44 |
+
temperature=0.7
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
return result[0]["generated_text"]
|
| 48 |
|
| 49 |
|
| 50 |
+
# Gradio GUI
|
| 51 |
with gr.Blocks(title="AI Model Demo") as demo:
|
| 52 |
gr.Markdown("# AI Model Demo")
|
| 53 |
gr.Markdown(
|
| 54 |
+
"An application using two pretrained models."
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
with gr.Tab("Sentiment Analysis"):
|
|
|
|
| 59 |
label="Enter a sentence",
|
| 60 |
placeholder="I love learning artificial intelligence!"
|
| 61 |
)
|
| 62 |
+
|
| 63 |
sentiment_button = gr.Button("Analyze Sentiment")
|
| 64 |
sentiment_output = gr.Textbox(label="Sentiment Result")
|
| 65 |
|
|
|
|
| 74 |
label="Enter a prompt",
|
| 75 |
placeholder="Artificial intelligence is"
|
| 76 |
)
|
| 77 |
+
|
| 78 |
generate_button = gr.Button("Generate Text")
|
| 79 |
generated_output = gr.Textbox(
|
| 80 |
label="Generated Text",
|
|
|
|
| 87 |
outputs=generated_output
|
| 88 |
)
|
| 89 |
|
| 90 |
+
demo.launch()
|
|
|
|
|
|
|
|
|