PraneshJs commited on
Commit
5602f0b
Β·
verified Β·
1 Parent(s): 683ef8e

Update gri.py

Browse files
Files changed (1) hide show
  1. gri.py +41 -41
gri.py CHANGED
@@ -1,42 +1,42 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
- from deep_translator import GoogleTranslator
4
- from langdetect import detect
5
- import torch
6
-
7
- MODEL_DIR = "model"
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
9
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
10
- emotion_labels = {
11
- 0: "Negative πŸ˜•",
12
- 1: "Neutral 😐",
13
- 2: "Positive πŸ™‚"
14
- }
15
- translator = GoogleTranslator(source='auto', target='en')
16
-
17
- def predict_emotion(text):
18
- detected_language = detect(text)
19
- if detected_language != 'en':
20
- translated_text = translator.translate(text)
21
- else:
22
- translated_text = text
23
- inputs = tokenizer(translated_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
24
- with torch.no_grad():
25
- outputs = model(**inputs)
26
- logits = outputs.logits
27
- predicted_class = torch.argmax(logits, dim=-1).item()
28
- emotion = emotion_labels.get(predicted_class, "Unknown")
29
- return emotion
30
-
31
- iface = gr.Interface(
32
- fn=predict_emotion,
33
- inputs=gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text"),
34
- outputs=[
35
- gr.Textbox(label="Predicted Sentiment")
36
- ],
37
- title="Emotion Detection App",
38
- description="Enter text in any language. The app will detect the language, translate if needed, and predict the emotion."
39
- )
40
-
41
- if __name__ == "__main__":
42
  iface.launch(share = True) # Set share=True to allow public access
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ from deep_translator import GoogleTranslator
4
+ from langdetect import detect
5
+ import torch
6
+
7
+ MODEL_DIR = "model"
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
9
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR, from_safetensors=True)
10
+ emotion_labels = {
11
+ 0: "Negative πŸ˜•",
12
+ 1: "Neutral 😐",
13
+ 2: "Positive πŸ™‚"
14
+ }
15
+ translator = GoogleTranslator(source='auto', target='en')
16
+
17
+ def predict_emotion(text):
18
+ detected_language = detect(text)
19
+ if detected_language != 'en':
20
+ translated_text = translator.translate(text)
21
+ else:
22
+ translated_text = text
23
+ inputs = tokenizer(translated_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
24
+ with torch.no_grad():
25
+ outputs = model(**inputs)
26
+ logits = outputs.logits
27
+ predicted_class = torch.argmax(logits, dim=-1).item()
28
+ emotion = emotion_labels.get(predicted_class, "Unknown")
29
+ return emotion
30
+
31
+ iface = gr.Interface(
32
+ fn=predict_emotion,
33
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here...", label="Input Text"),
34
+ outputs=[
35
+ gr.Textbox(label="Predicted Sentiment")
36
+ ],
37
+ title="Emotion Detection App",
38
+ description="Enter text in any language. The app will detect the language, translate if needed, and predict the emotion."
39
+ )
40
+
41
+ if __name__ == "__main__":
42
  iface.launch(share = True) # Set share=True to allow public access