ktopwal commited on
Commit
935fd9c
·
verified ·
1 Parent(s): 73914ef

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +90 -1
index.html CHANGED
@@ -19,7 +19,96 @@
19
  </gradio-requirements>
20
 
21
  <gradio-file name="app.py" entrypoint>
22
- <!-- Replace this comment with your actual Python code -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  </gradio-file>
24
  </body>
25
  </html>
 
19
  </gradio-requirements>
20
 
21
  <gradio-file name="app.py" entrypoint>
22
+ import gradio as gr
23
+ from transformers_js_py import import_transformers_js
24
+
25
+ # Load Transformers.js
26
+ transformers = await import_transformers_js()
27
+ pipeline = transformers.pipeline
28
+
29
+ # Model 1: Sentiment Analysis
30
+ sentiment_model = await pipeline(
31
+ "sentiment-analysis",
32
+ "Xenova/distilbert-base-uncased-finetuned-sst-2-english"
33
+ )
34
+
35
+ # Model 2: Text Generation
36
+ text_model = await pipeline(
37
+ "text-generation",
38
+ "Xenova/gpt2"
39
+ )
40
+
41
+
42
+ # Callback 1: Sentiment Analysis
43
+ async def analyze_sentiment(text):
44
+ if not text or not text.strip():
45
+ return "Please enter some text."
46
+
47
+ result = await sentiment_model(text)
48
+ result = result[0]
49
+
50
+ return (
51
+ f"Sentiment: {result['label']}\n"
52
+ f"Confidence: {result['score']:.2%}"
53
+ )
54
+
55
+
56
+ # Callback 2: Text Generation
57
+ async def generate_text(prompt):
58
+ if not prompt or not prompt.strip():
59
+ return "Please enter a prompt."
60
+
61
+ result = await text_model(
62
+ prompt,
63
+ max_new_tokens=40,
64
+ do_sample=True,
65
+ temperature=0.7
66
+ )
67
+
68
+ return result[0]["generated_text"]
69
+
70
+
71
+ # Gradio GUI
72
+ with gr.Blocks(title="AI Model Demo") as demo:
73
+ gr.Markdown("# AI Model Demo")
74
+ gr.Markdown(
75
+ "An application using two pretrained models."
76
+ )
77
+
78
+ with gr.Tab("Sentiment Analysis"):
79
+ sentiment_input = gr.Textbox(
80
+ label="Enter a sentence",
81
+ placeholder="I love learning artificial intelligence!"
82
+ )
83
+
84
+ sentiment_button = gr.Button("Analyze Sentiment")
85
+ sentiment_output = gr.Textbox(label="Sentiment Result")
86
+
87
+ sentiment_button.click(
88
+ fn=analyze_sentiment,
89
+ inputs=sentiment_input,
90
+ outputs=sentiment_output
91
+ )
92
+
93
+ with gr.Tab("Text Generation"):
94
+ prompt_input = gr.Textbox(
95
+ label="Enter a prompt",
96
+ placeholder="Artificial intelligence is"
97
+ )
98
+
99
+ generate_button = gr.Button("Generate Text")
100
+ generated_output = gr.Textbox(
101
+ label="Generated Text",
102
+ lines=5
103
+ )
104
+
105
+ generate_button.click(
106
+ fn=generate_text,
107
+ inputs=prompt_input,
108
+ outputs=generated_output
109
+ )
110
+
111
+ demo.launch()
112
  </gradio-file>
113
  </body>
114
  </html>