Spaces:
Runtime error
Runtime error
File size: 2,741 Bytes
1398925 ab07088 1398925 ab07088 6908aad bf83ac1 d86acb9 bf83ac1 d86acb9 bf83ac1 3bc002c bf83ac1 ab07088 6908aad ab07088 6908aad ab07088 d86acb9 07a4801 13081ab ab07088 6908aad ab07088 6908aad af1aba9 ab07088 6908aad 3bc002c d86acb9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer, GPT2LMHeadModel, GPT2Tokenizer, AutoTokenizer, AutoModelForSequenceClassification
import torch
# Translation
def translate(text, target_language):
language_codes = {
"Spanish": "es",
"French (European)": "fr",
"French (Canadian)": "fr",
"Italian": "it",
"Ukrainian": "uk",
"Portuguese (Brazilian)": "pt_BR",
"Portuguese (European)": "pt",
"Russian": "ru",
"Chinese": "zh",
"Dutch": "nl",
"German": "de",
"Arabic": "ar",
"Hebrew": "he",
"Greek": "el"
}
# Text Generation
def generate_text(prompt):
text_gen = pipeline("text-generation", model="gpt2")
generated_text = text_gen(prompt, max_length=max_length, do_sample=True)[0]["generated_text"]
return generated_text
# Text Classification
def classify_text(text):
classifier = pipeline("zero-shot-classification")
result = classifier(text, labels.split(','))
scores = result["scores"]
predictions = result["labels"]
sorted_predictions = [pred for _, pred in sorted(zip(scores, predictions), reverse=True)]
return sorted_predictions
# Sentiment Analysis
def sentiment_analysis(text):
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
sentiment_scores = torch.softmax(outputs.logits, dim=1)
sentiment = "positive" if sentiment_scores[0, 1] > sentiment_scores[0, 0] else "negative"
return sentiment
language_options = [
"Spanish", "French (European)", "French (Canadian)", "Italian", "Ukrainian",
"Portuguese (Brazilian)", "Portuguese (European)", "Russian", "Chinese",
"Dutch", "German", "Arabic", "Hebrew", "Greek"
]
iface = gr.Interface(
[translate, generate_text, classify_text, sentiment_analysis],
inputs=[
gr.inputs.Textbox(lines=5, label="Enter text to translate:"),
gr.inputs.Dropdown(choices=language_options, label="Target Language"),
gr.inputs.Textbox(lines=5, label="Enter text for text generation:"),
gr.inputs.Textbox(lines=5, label="Enter text for text classification:"),
gr.inputs.Textbox(lines=5, label="Enter text for sentiment analysis:"),
],
outputs=[
gr.outputs.Textbox(label="Translated Text"),
gr.outputs.Textbox(label="Generated Text"),
gr.outputs.Textbox(label="Classification Result"),
gr.outputs.Textbox(label="Sentiment Result"),
],
)
iface.launch()
|