Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,36 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline, pipeline
|
5 |
from langdetect import detect
|
6 |
from matplotlib import pyplot as plt
|
7 |
import imageio
|
8 |
|
9 |
-
"""
|
10 |
-
def greet(name):
|
11 |
-
return "Hello " + name + "!!"
|
12 |
-
|
13 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
14 |
-
iface.launch()
|
15 |
-
"""
|
16 |
# Load the model
|
17 |
-
model = AutoModelForSequenceClassification.from_pretrained("saved_model")
|
18 |
tokenizer = AutoTokenizer.from_pretrained("saved_model")
|
19 |
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer)
|
20 |
|
21 |
-
|
22 |
# Function called by the UI
|
23 |
def attribution(text):
|
|
|
24 |
# Clean the plot
|
25 |
plt.clf()
|
26 |
-
|
27 |
# Detect the language
|
28 |
language = detect(text)
|
29 |
-
|
30 |
# Translate the input in german if necessary
|
31 |
if language == 'fr':
|
32 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-de")
|
33 |
-
translatedText = translator(text[0:
|
34 |
text = translatedText[0]["translation_text"]
|
35 |
-
|
|
|
|
|
36 |
# Set the bars of the bar chart
|
37 |
bars = ""
|
38 |
if language == 'fr':
|
@@ -40,17 +38,17 @@ def attribution(text):
|
|
40 |
else:
|
41 |
bars = ("VBS", "EDI", "AB-BA", "EJPD", "WBF", "UVEK", "EDA", "Parl", "BK", "EFD", "BV", "BGer")
|
42 |
|
43 |
-
# Make the prediction with the
|
44 |
-
results = pipe(text[0:
|
45 |
rates = [row["score"] for row in results[0]]
|
46 |
-
|
47 |
# Bar chart
|
48 |
y_pos = np.arange(len(bars))
|
49 |
plt.barh(y_pos, rates)
|
50 |
plt.yticks(y_pos, bars)
|
51 |
-
|
52 |
# Set the output text
|
53 |
-
name = ""
|
54 |
maxRate = np.max(rates)
|
55 |
maxIndex = np.argmax(rates)
|
56 |
|
@@ -72,18 +70,17 @@ def attribution(text):
|
|
72 |
i = 1
|
73 |
# ML model pretty sure, show only one department
|
74 |
else:
|
75 |
-
name =
|
76 |
-
|
77 |
# Save the bar chart as png and load it (enables better display)
|
78 |
plt.savefig('rates.png')
|
79 |
im = imageio.imread('rates.png')
|
80 |
|
81 |
return name, im
|
82 |
-
|
83 |
|
84 |
# display the UI
|
85 |
interface = gr.Interface(fn=attribution, layout="vertical",
|
86 |
-
inputs=[gr.inputs.Textbox(lines=20,
|
87 |
-
|
88 |
-
outputs=['text', 'image'])
|
89 |
interface.launch()
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
import gradio as gr
|
5 |
import numpy as np
|
6 |
+
import requests
|
7 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline, pipeline
|
8 |
from langdetect import detect
|
9 |
from matplotlib import pyplot as plt
|
10 |
import imageio
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Load the model
|
13 |
+
model = AutoModelForSequenceClassification.from_pretrained("saved_model")
|
14 |
tokenizer = AutoTokenizer.from_pretrained("saved_model")
|
15 |
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer)
|
16 |
|
|
|
17 |
# Function called by the UI
|
18 |
def attribution(text):
|
19 |
+
|
20 |
# Clean the plot
|
21 |
plt.clf()
|
22 |
+
|
23 |
# Detect the language
|
24 |
language = detect(text)
|
25 |
+
|
26 |
# Translate the input in german if necessary
|
27 |
if language == 'fr':
|
28 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-de")
|
29 |
+
translatedText = translator(text[0:1000])
|
30 |
text = translatedText[0]["translation_text"]
|
31 |
+
elif language != 'de':
|
32 |
+
return "The language is not recognized, it must be either in German or in French.", None
|
33 |
+
|
34 |
# Set the bars of the bar chart
|
35 |
bars = ""
|
36 |
if language == 'fr':
|
|
|
38 |
else:
|
39 |
bars = ("VBS", "EDI", "AB-BA", "EJPD", "WBF", "UVEK", "EDA", "Parl", "BK", "EFD", "BV", "BGer")
|
40 |
|
41 |
+
# Make the prediction with the 1000 first characters
|
42 |
+
results = pipe(text[0:1000], return_all_scores=True)
|
43 |
rates = [row["score"] for row in results[0]]
|
44 |
+
|
45 |
# Bar chart
|
46 |
y_pos = np.arange(len(bars))
|
47 |
plt.barh(y_pos, rates)
|
48 |
plt.yticks(y_pos, bars)
|
49 |
+
|
50 |
# Set the output text
|
51 |
+
name = ""
|
52 |
maxRate = np.max(rates)
|
53 |
maxIndex = np.argmax(rates)
|
54 |
|
|
|
70 |
i = 1
|
71 |
# ML model pretty sure, show only one department
|
72 |
else:
|
73 |
+
name = str(maxRate)[2:4] + "%" + "\t\t\t\t\t\t" + bars[maxIndex]
|
74 |
+
|
75 |
# Save the bar chart as png and load it (enables better display)
|
76 |
plt.savefig('rates.png')
|
77 |
im = imageio.imread('rates.png')
|
78 |
|
79 |
return name, im
|
80 |
+
|
81 |
|
82 |
# display the UI
|
83 |
interface = gr.Interface(fn=attribution, layout="vertical",
|
84 |
+
inputs=[gr.inputs.Textbox(lines=20, placeholder="Geben Sie bitte den Titel und den Sumbmitted Text des Vorstoss ein.\nVeuillez entrer le titre et le Submitted Text de la requête.")],
|
85 |
+
outputs=['text', 'image'])
|
|
|
86 |
interface.launch()
|