Manojnayakmit commited on
Commit
0c66a04
·
verified ·
1 Parent(s): ff16123

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # Use CPU on Hugging Face Spaces free tier
6
+ DEVICE = -1
7
+
8
+ # Lazy loading (loads model only when first used)
9
+ models = {}
10
+
11
+ def get_model(task_name):
12
+ if task_name not in models:
13
+ if task_name == "Chatbot":
14
+ models[task_name] = pipeline(
15
+ "text-generation",
16
+ model="microsoft/DialoGPT-small",
17
+ device=DEVICE
18
+ )
19
+
20
+ elif task_name == "Sentiment Analysis":
21
+ models[task_name] = pipeline(
22
+ "sentiment-analysis",
23
+ model="distilbert-base-uncased-finetuned-sst-2-english",
24
+ device=DEVICE
25
+ )
26
+
27
+ elif task_name == "NER":
28
+ models[task_name] = pipeline(
29
+ "token-classification",
30
+ model="dslim/bert-base-NER",
31
+ aggregation_strategy="simple",
32
+ device=DEVICE
33
+ )
34
+
35
+ elif task_name == "Summarization":
36
+ models[task_name] = pipeline(
37
+ "summarization",
38
+ model="sshleifer/distilbart-cnn-12-6",
39
+ device=DEVICE
40
+ )
41
+
42
+ elif task_name == "Translation (EN→FR)":
43
+ models[task_name] = pipeline(
44
+ "translation_en_to_fr",
45
+ model="Helsinki-NLP/opus-mt-en-fr",
46
+ device=DEVICE
47
+ )
48
+
49
+ return models[task_name]
50
+
51
+
52
+ def run_task(task, user_input, chat_history):
53
+
54
+ if not user_input.strip():
55
+ return "Please enter some text.", chat_history
56
+
57
+ model = get_model(task)
58
+
59
+ if task == "Chatbot":
60
+ response = model(
61
+ user_input,
62
+ max_new_tokens=100,
63
+ pad_token_id=50256
64
+ )
65
+
66
+ bot_reply = response[0]["generated_text"]
67
+ chat_history = chat_history + [(user_input, bot_reply)]
68
+ return "", chat_history
69
+
70
+ elif task == "Sentiment Analysis":
71
+ sentiment = model(user_input)[0]
72
+ result = f"Label: {sentiment['label']}\nConfidence: {sentiment['score']:.2f}"
73
+ return result, chat_history
74
+
75
+ elif task == "Summarization":
76
+ summary = model(
77
+ user_input,
78
+ max_length=120,
79
+ min_length=40,
80
+ do_sample=False
81
+ )[0]["summary_text"]
82
+ return summary, chat_history
83
+
84
+ elif task == "NER":
85
+ entities = model(user_input)
86
+ if not entities:
87
+ return "No entities found.", chat_history
88
+
89
+ formatted = "\n".join(
90
+ f"{e['word']} ({e['entity_group']}) - {e['score']:.2f}"
91
+ for e in entities
92
+ )
93
+ return formatted, chat_history
94
+
95
+ elif task == "Translation (EN→FR)":
96
+ translation = model(user_input)[0]["translation_text"]
97
+ return translation, chat_history
98
+
99
+
100
+ with gr.Blocks(title="NLP Application") as demo:
101
+
102
+ gr.Markdown("# NLP Application")
103
+
104
+ task_dropdown = gr.Dropdown(
105
+ choices=[
106
+ "Chatbot",
107
+ "Sentiment Analysis",
108
+ "NER",
109
+ "Summarization",
110
+ "Translation (EN→FR)"
111
+ ],
112
+ label="Select NLP Task"
113
+ )
114
+
115
+ user_input = gr.Textbox(
116
+ lines=5,
117
+ placeholder="Enter text here...",
118
+ label="Input Text"
119
+ )
120
+
121
+ output_box = gr.Textbox(
122
+ label="Output"
123
+ )
124
+
125
+ chatbot = gr.Chatbot(label="Conversation")
126
+
127
+ state = gr.State([])
128
+
129
+ run_button = gr.Button("Run")
130
+
131
+ run_button.click(
132
+ fn=run_task,
133
+ inputs=[task_dropdown, user_input, state],
134
+ outputs=[output_box, chatbot]
135
+ )
136
+
137
+ demo.launch()