Update app.py
Browse files
app.py
CHANGED
@@ -1,81 +1,86 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
|
5 |
-
with open('results.json', 'r') as f:
|
6 |
-
results_data = json.load(f)
|
7 |
-
|
8 |
-
with open('sarcasm_data.json', 'r') as f:
|
9 |
-
sarcasm_data = json.load(f)
|
10 |
-
|
11 |
-
def get_dialogues_and_result(id):
|
12 |
-
|
13 |
-
if not
|
14 |
-
return f"ID {id} not found in results.json"
|
15 |
-
|
16 |
-
sarcasm_info = sarcasm_data.get(id)
|
17 |
-
if not sarcasm_info:
|
18 |
-
return f"ID {id} not found in sarcasm_data.json"
|
19 |
-
|
20 |
-
utterance = f"{sarcasm_info['speaker']}: {sarcasm_info['utterance']}"
|
21 |
-
context = "\n".join(
|
22 |
-
f"{speaker}: {line}" for speaker, line in zip(sarcasm_info['context_speakers'], sarcasm_info['context'])
|
23 |
-
)
|
24 |
-
|
25 |
-
output = {
|
26 |
-
"id": id,
|
27 |
-
"utterance": utterance,
|
28 |
-
"context": context,
|
29 |
-
"
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
output["
|
41 |
-
output["
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
|
5 |
+
with open('results.json', 'r') as f:
|
6 |
+
results_data = json.load(f)
|
7 |
+
|
8 |
+
with open('sarcasm_data.json', 'r') as f:
|
9 |
+
sarcasm_data = json.load(f)
|
10 |
+
|
11 |
+
def get_dialogues_and_result(id):
|
12 |
+
pred = next((item for item in results_data if item['id'] == id), None)
|
13 |
+
if not pred:
|
14 |
+
return f"ID {id} not found in results.json"
|
15 |
+
|
16 |
+
sarcasm_info = sarcasm_data.get(id)
|
17 |
+
if not sarcasm_info:
|
18 |
+
return f"ID {id} not found in sarcasm_data.json"
|
19 |
+
|
20 |
+
utterance = f"{sarcasm_info['speaker']}: {sarcasm_info['utterance']}"
|
21 |
+
context = "\n".join(
|
22 |
+
f"{speaker}: {line}" for speaker, line in zip(sarcasm_info['context_speakers'], sarcasm_info['context'])
|
23 |
+
)
|
24 |
+
|
25 |
+
output = {
|
26 |
+
"id": id,
|
27 |
+
"utterance": utterance,
|
28 |
+
"context": context,
|
29 |
+
"ground_truth": int(sarcasm_info['sarcasm']),
|
30 |
+
"pred": pred['sarcasm']
|
31 |
+
}
|
32 |
+
|
33 |
+
return output
|
34 |
+
|
35 |
+
def analyze_videos(utterance_filename):
|
36 |
+
utterance_id = os.path.splitext(os.path.basename(utterance_filename))[0]
|
37 |
+
output = get_dialogues_and_result(utterance_id)
|
38 |
+
|
39 |
+
return (
|
40 |
+
output["context"],
|
41 |
+
output["utterance"],
|
42 |
+
output["ground_truth"],
|
43 |
+
output["pred"]
|
44 |
+
)
|
45 |
+
|
46 |
+
def custom_css():
|
47 |
+
return """
|
48 |
+
.gradio-heading {
|
49 |
+
text-align: center;
|
50 |
+
color: #0056b3;
|
51 |
+
font-size: 28px;
|
52 |
+
font-weight: bold;
|
53 |
+
margin-bottom: 20px;
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
|
57 |
+
css_style = custom_css()
|
58 |
+
|
59 |
+
with gr.Blocks(css=css_style) as interface:
|
60 |
+
gr.Markdown("<div class='gradio-heading'>Sarcasm Detection</div>")
|
61 |
+
with gr.Row():
|
62 |
+
with gr.Column(scale=1):
|
63 |
+
context_video_input = gr.Video(label="Upload Context Video")
|
64 |
+
punchline_video_input = gr.Video(label="Upload Punchline Video")
|
65 |
+
with gr.Column(scale=2):
|
66 |
+
context_dialogue_output = gr.Textbox(label="Context Dialogues")
|
67 |
+
punchline_dialogue_output = gr.Textbox(label="Punchline Dialogue")
|
68 |
+
with gr.Column():
|
69 |
+
punchline_sarcasm_true = gr.Label(label="Ground Truth")
|
70 |
+
punchline_sarcasm_pred = gr.Label(label="Sarcasm Detected")
|
71 |
+
|
72 |
+
submit_button = gr.Button("Submit")
|
73 |
+
|
74 |
+
submit_button.click(
|
75 |
+
fn=analyze_videos,
|
76 |
+
inputs=[punchline_video_input],
|
77 |
+
outputs=[
|
78 |
+
context_dialogue_output,
|
79 |
+
punchline_dialogue_output,
|
80 |
+
punchline_sarcasm_true,
|
81 |
+
punchline_sarcasm_pred
|
82 |
+
]
|
83 |
+
)
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
interface.launch()
|