Spaces:
Sleeping
Sleeping
koptelovmax
commited on
Commit
•
7e0562e
1
Parent(s):
3bf0a5e
Code update
Browse files
app.py
CHANGED
@@ -12,6 +12,18 @@ import amrlib
|
|
12 |
from amrlib.graph_processing.annotator import add_lemmas
|
13 |
from amrlib.alignments.rbw_aligner import RBWAligner
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Find a node corresponding targetWord in the graph:
|
16 |
def getTargetWordNode(segmentTokens, aligner, alignments, target):
|
17 |
# Get target word in English:
|
@@ -102,15 +114,6 @@ def getTargetWordSubGraphFullPath(amrGraph, target):
|
|
102 |
def main():
|
103 |
st.header('Abstract Meaning Representation based summary of French text', divider='blue')
|
104 |
|
105 |
-
#segmentFr = st.text_area(
|
106 |
-
#"Text to summarize:",
|
107 |
-
#"Article 1 : Occupations ou utilisations du sol interdites\n\n"
|
108 |
-
#"1) Dans l’ensemble de la zone sont interdits :\n\n"
|
109 |
-
#"Les pylônes et poteaux, supports d’enseignes et d’antennes d’émission ou de réception de \n"
|
110 |
-
#"signaux radioélectriques.",
|
111 |
-
#height=170,
|
112 |
-
#)#.replace('\n',' ')
|
113 |
-
|
114 |
segmentFr = st.text_area(
|
115 |
"Text to summarize:",
|
116 |
"Article 2 : Occupations ou utilisations du sol soumises à des conditions particulières\n\n"
|
@@ -127,28 +130,21 @@ def main():
|
|
127 |
#height=170,
|
128 |
#)
|
129 |
|
130 |
-
#targetWord = st.text_input('Keyword:', 'Occupations')
|
131 |
targetWord = st.text_input('Keyword:', 'clôtures')
|
132 |
##targetWord = st.text_input('Keyword:', 'compromettre')
|
133 |
|
134 |
if st.button('Summarize'):
|
135 |
# Fix input formatting:
|
136 |
segmentFr = segmentFr.replace('\n',' ')
|
137 |
-
|
138 |
-
#st.code(segmentFr)
|
139 |
-
|
140 |
# Translate segment into English:
|
141 |
-
model =
|
142 |
-
#segmentEn = model.translate(segmentFr.lower() , source_lang='fr', target_lang='en')
|
143 |
segmentEn = model.translate(segmentFr , source_lang='fr', target_lang='en')
|
144 |
|
145 |
# Get an AMR graph:
|
146 |
-
stog =
|
147 |
inputGraph = stog.parse_sents([segmentEn])
|
148 |
|
149 |
-
## Output the resulting graph:
|
150 |
-
#print(inputGraph[0])
|
151 |
-
|
152 |
# Get tokenized representation of segment in French:
|
153 |
segmentFrTokens = word_tokenize(segmentFr, language='french')
|
154 |
|
@@ -178,10 +174,9 @@ def main():
|
|
178 |
if not errorFlag:
|
179 |
if targetNode != 'Error!':
|
180 |
targetSubGraph = getTargetWordSubGraphFullPath(inputGraph[0], targetNode)
|
181 |
-
#print(targetSubGraph)
|
182 |
|
183 |
# Generate text from given AMR-graph:
|
184 |
-
gtos =
|
185 |
rulesEn, _ = gtos.generate([targetSubGraph])
|
186 |
|
187 |
# Remove "1." from the text:
|
@@ -189,15 +184,11 @@ def main():
|
|
189 |
|
190 |
# Translate it back to French
|
191 |
rulesFr = model.translate(rulesEn[0], source_lang='en', target_lang='fr')
|
192 |
-
#print(rulesEn[0])
|
193 |
-
#print(rulesFr)
|
194 |
|
195 |
st.write("Summary: ", rulesFr)
|
196 |
else:
|
197 |
-
#print('Alignment between target word in French and its English instance not found')
|
198 |
st.write('Error! Alignment between target word in French and its English instance not found')
|
199 |
else:
|
200 |
-
#print('Error! Cannot find keyword in the graph')
|
201 |
st.write('Error! Cannot find keyword in the graph')
|
202 |
|
203 |
if __name__ == "__main__":
|
|
|
12 |
from amrlib.graph_processing.annotator import add_lemmas
|
13 |
from amrlib.alignments.rbw_aligner import RBWAligner
|
14 |
|
15 |
+
@st.cache_resource
|
16 |
+
def load_easynmt():
|
17 |
+
return EasyNMT('opus-mt')
|
18 |
+
|
19 |
+
@st.cache_resource
|
20 |
+
def load_stog_model():
|
21 |
+
return amrlib.load_stog_model(model_dir='model_stog')
|
22 |
+
|
23 |
+
@st.cache_resource
|
24 |
+
def load_gtos_model():
|
25 |
+
return amrlib.load_gtos_model(model_dir='model_gtos')
|
26 |
+
|
27 |
# Find a node corresponding targetWord in the graph:
|
28 |
def getTargetWordNode(segmentTokens, aligner, alignments, target):
|
29 |
# Get target word in English:
|
|
|
114 |
def main():
|
115 |
st.header('Abstract Meaning Representation based summary of French text', divider='blue')
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
segmentFr = st.text_area(
|
118 |
"Text to summarize:",
|
119 |
"Article 2 : Occupations ou utilisations du sol soumises à des conditions particulières\n\n"
|
|
|
130 |
#height=170,
|
131 |
#)
|
132 |
|
|
|
133 |
targetWord = st.text_input('Keyword:', 'clôtures')
|
134 |
##targetWord = st.text_input('Keyword:', 'compromettre')
|
135 |
|
136 |
if st.button('Summarize'):
|
137 |
# Fix input formatting:
|
138 |
segmentFr = segmentFr.replace('\n',' ')
|
139 |
+
|
|
|
|
|
140 |
# Translate segment into English:
|
141 |
+
model = load_easynmt()
|
|
|
142 |
segmentEn = model.translate(segmentFr , source_lang='fr', target_lang='en')
|
143 |
|
144 |
# Get an AMR graph:
|
145 |
+
stog = load_stog_model()
|
146 |
inputGraph = stog.parse_sents([segmentEn])
|
147 |
|
|
|
|
|
|
|
148 |
# Get tokenized representation of segment in French:
|
149 |
segmentFrTokens = word_tokenize(segmentFr, language='french')
|
150 |
|
|
|
174 |
if not errorFlag:
|
175 |
if targetNode != 'Error!':
|
176 |
targetSubGraph = getTargetWordSubGraphFullPath(inputGraph[0], targetNode)
|
|
|
177 |
|
178 |
# Generate text from given AMR-graph:
|
179 |
+
gtos = load_gtos_model()
|
180 |
rulesEn, _ = gtos.generate([targetSubGraph])
|
181 |
|
182 |
# Remove "1." from the text:
|
|
|
184 |
|
185 |
# Translate it back to French
|
186 |
rulesFr = model.translate(rulesEn[0], source_lang='en', target_lang='fr')
|
|
|
|
|
187 |
|
188 |
st.write("Summary: ", rulesFr)
|
189 |
else:
|
|
|
190 |
st.write('Error! Alignment between target word in French and its English instance not found')
|
191 |
else:
|
|
|
192 |
st.write('Error! Cannot find keyword in the graph')
|
193 |
|
194 |
if __name__ == "__main__":
|