Spaces:
Runtime error
Runtime error
Create app.py main script
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import gradio as gr
|
2 |
+
from frame_semantic_transformer import FrameSemanticTransformer
|
3 |
+
from transformers import T5ForConditionalGeneration, T5TokenizerFast
|
4 |
+
from frame_semantic_transformer.data.frame_types import Frame
|
5 |
+
from frame_semantic_transformer.data.loaders.loader import InferenceLoader
|
6 |
+
import pandas as pd
|
7 |
+
import ast
|
8 |
+
import re
|
9 |
+
from nltk.stem import SnowballStemmer
|
10 |
+
|
11 |
+
# Huggingface model path
|
12 |
+
huggingface_model_path = "nelsonjq/frame-semantic-transformer-french-small"
|
13 |
+
|
14 |
+
# Load the model and tokenizer from Huggingface
|
15 |
+
model = T5ForConditionalGeneration.from_pretrained(huggingface_model_path)
|
16 |
+
tokenizer = T5TokenizerFast.from_pretrained(huggingface_model_path)
|
17 |
+
|
18 |
+
# Load the DataFrame
|
19 |
+
!mkdir -p Asfalda
|
20 |
+
!wget --output-document Asfalda/frame_lus_df.tsv https://seafile.unistra.fr/f/0155ced00b8d441eb131/?dl=1
|
21 |
+
frame_lus_df = pd.read_csv("Asfalda/frame_lus_df.tsv", delimiter='\t')
|
22 |
+
|
23 |
+
# Filter out 'Other_sense' and normalize frame names
|
24 |
+
frame_lus_df = frame_lus_df[frame_lus_df['Name'] != 'Other_sense']
|
25 |
+
frame_lus_df['Name'] = frame_lus_df['Name'].apply(lambda x: x.split(".")[0])
|
26 |
+
frame_lus_df['Name'] = frame_lus_df['Name'].str.replace(r'^[Ff][Rr][Vv]_', '', regex=True)
|
27 |
+
|
28 |
+
# Add missing frame names
|
29 |
+
new_row_Suasion = {
|
30 |
+
'Name': 'Suasion',
|
31 |
+
'Core_Elms': "['Content', 'Cognizer', 'Persuader', 'Target', 'Text', 'Action', 'Addressee']",
|
32 |
+
'Non_Core_Elms': "['Speaker', 'Topic']",
|
33 |
+
'Lus': "['convaincre.v', 'convertir.v', 'persuader.v', 'convaincant.a', 'persuasion.n', 'dissuader.v', 'apprendre.v', 'dissuasion.n', 'décider.v']",
|
34 |
+
'Lus_simple': "['convaincre', 'convertir', 'persuader', 'convaincant', 'persuasion', 'dissuader', 'apprendre', 'dissuasion', 'décider']"
|
35 |
+
}
|
36 |
+
|
37 |
+
new_row_Arriving = {
|
38 |
+
'Name': 'Arriving',
|
39 |
+
'Core_Elms': "['Theme', 'Goal']",
|
40 |
+
'Non_Core_Elms': "['Target', 'Means']",
|
41 |
+
'Lus': "['gagner.v']",
|
42 |
+
'Lus_simple': "['gagner']"
|
43 |
+
}
|
44 |
+
|
45 |
+
new_row_Suasion_df = pd.DataFrame([new_row_Suasion])
|
46 |
+
new_row_Arriving_df = pd.DataFrame([new_row_Arriving])
|
47 |
+
|
48 |
+
frame_lus_df = pd.concat([frame_lus_df, new_row_Suasion_df, new_row_Arriving_df], ignore_index=True)
|
49 |
+
|
50 |
+
# Define FrenchInferenceLoader
|
51 |
+
french_stemmer = SnowballStemmer("french")
|
52 |
+
|
53 |
+
def extract_frame(df_row_frame) -> Frame:
|
54 |
+
name = df_row_frame['Name']
|
55 |
+
core_elms = ast.literal_eval(df_row_frame['Core_Elms'])
|
56 |
+
non_core_elms = ast.literal_eval(df_row_frame['Non_Core_Elms'])
|
57 |
+
lus = ast.literal_eval(df_row_frame['Lus'])
|
58 |
+
return Frame(name=name, core_elements=core_elms, non_core_elements=non_core_elms, lexical_units=lus)
|
59 |
+
|
60 |
+
class FrenchInferenceLoader(InferenceLoader):
|
61 |
+
def __init__(self, french_framenet_df_file):
|
62 |
+
self.frames = []
|
63 |
+
for index, row in french_framenet_df_file.iterrows():
|
64 |
+
frame = extract_frame(row)
|
65 |
+
self.frames.append(frame)
|
66 |
+
|
67 |
+
def load_frames(self):
|
68 |
+
return self.frames
|
69 |
+
|
70 |
+
def normalize_lexical_unit_text(self, lu: str) -> str:
|
71 |
+
normalized_lu = lu.lower()
|
72 |
+
if '.' in normalized_lu:
|
73 |
+
normalized_lu = normalized_lu.split('.')[0]
|
74 |
+
normalized_lu = re.sub(r"[^a-z0-9 ]", "", normalized_lu)
|
75 |
+
return french_stemmer.stem(normalized_lu)
|
76 |
+
|
77 |
+
# Initialize the FrenchInferenceLoader and FrameSemanticTransformer
|
78 |
+
inference_loader = FrenchInferenceLoader(frame_lus_df)
|
79 |
+
transformer = FrameSemanticTransformer(huggingface_model_path, inference_loader=inference_loader)
|
80 |
+
|
81 |
+
# Function to process the input sentence and display frame detection results
|
82 |
+
def detect_frames_in_text(input_text):
|
83 |
+
result = transformer.detect_frames(input_text)
|
84 |
+
output = f"Results found in the sentence:\n\n{result.sentence}\n"
|
85 |
+
for frame in result.frames:
|
86 |
+
output += f"\nFRAME: {frame.name}\n\nFrame Elements:\n"
|
87 |
+
for element in frame.frame_elements:
|
88 |
+
output += f"\t\t{element.name}: {element.text}\n"
|
89 |
+
return output
|
90 |
+
|
91 |
+
# Gradio interface
|
92 |
+
iface = gr.Interface(
|
93 |
+
fn=detect_frames_in_text,
|
94 |
+
inputs="text",
|
95 |
+
outputs="text",
|
96 |
+
title="French Frame Detection App",
|
97 |
+
description="Enter a French sentence to detect frames and frame elements using the FrameSemanticTransformer model."
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch the app
|
101 |
+
iface.launch()
|