LovnishVerma commited on
Commit
55a67a5
1 Parent(s): 85c04da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -1,25 +1,34 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Load pre-trained sentiment analysis model
5
- classifier = pipeline('sentiment-analysis')
 
 
6
 
7
- # Define the sentiment analysis function
8
- def analyze_sentiment(text):
9
- result = classifier(text)
10
- sentiment = result[0]['label']
11
- confidence = result[0]['score']
12
- return f"Sentiment: {sentiment}, Confidence: {confidence:.4f}"
 
 
 
 
 
 
13
 
14
  # Create Gradio interface
15
  iface = gr.Interface(
16
- fn=analyze_sentiment,
17
  inputs=gr.Textbox(),
18
  outputs=gr.Textbox(),
19
  live=True,
20
- title="Sentiment Analysis App",
21
- description="Enter a sentence, and the model will predict its sentiment.",
22
  )
23
 
24
  # Launch the Gradio app
25
  iface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
 
4
+ # Load pre-trained MarianMT model and tokenizer for translation
5
+ model_name = "Helsinki-NLP/opus-mt-en-de"
6
+ model = MarianMTModel.from_pretrained(model_name)
7
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
8
 
9
+ # Define the translation function
10
+ def translate_text(text):
11
+ # Tokenize the input text
12
+ inputs = tokenizer(text, return_tensors="pt")
13
+
14
+ # Perform translation
15
+ translation = model.generate(**inputs)
16
+
17
+ # Decode the translated text
18
+ translated_text = tokenizer.decode(translation[0], skip_special_tokens=True)
19
+
20
+ return translated_text
21
 
22
  # Create Gradio interface
23
  iface = gr.Interface(
24
+ fn=translate_text,
25
  inputs=gr.Textbox(),
26
  outputs=gr.Textbox(),
27
  live=True,
28
+ title="Language Translation App",
29
+ description="Translate text from English to German using MarianMT.",
30
  )
31
 
32
  # Launch the Gradio app
33
  iface.launch()
34
+