SmitaGautam commited on
Commit
8e15a23
·
verified ·
1 Parent(s): 30d7990

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -81
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
- result = next((item for item in results_data if item['id'] == id), None)
13
- if not result:
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
- "sarcasm": result['sarcasm']
30
- }
31
-
32
- return output
33
-
34
- def analyze_videos(utterance_filename):
35
- utterance_id = os.path.splitext(os.path.basename(utterance_filename))[0]
36
- output = get_dialogues_and_result(utterance_id)
37
-
38
- return (
39
- output["context"],
40
- output["utterance"],
41
- output["sarcasm"]
42
- )
43
-
44
- def custom_css():
45
- return """
46
- .gradio-heading {
47
- text-align: center;
48
- color: #0056b3;
49
- font-size: 28px;
50
- font-weight: bold;
51
- margin-bottom: 20px;
52
- }
53
- """
54
-
55
- css_style = custom_css()
56
-
57
- with gr.Blocks(css=css_style) as interface:
58
- gr.Markdown("<div class='gradio-heading'>Sarcasm Detection Tool</div>")
59
- with gr.Row():
60
- with gr.Column(scale=1):
61
- context_video_input = gr.Video(label="Upload Context Video")
62
- punchline_video_input = gr.Video(label="Upload Punchline Video")
63
- with gr.Column(scale=2):
64
- context_dialogue_output = gr.Textbox(label="Context Dialogues")
65
- punchline_dialogue_output = gr.Textbox(label="Punchline Dialogue")
66
- punchline_sarcasm_output = gr.Label(label="Sarcasm")
67
-
68
- submit_button = gr.Button("Submit")
69
-
70
- submit_button.click(
71
- fn=analyze_videos,
72
- inputs=[punchline_video_input],
73
- outputs=[
74
- context_dialogue_output,
75
- punchline_dialogue_output,
76
- punchline_sarcasm_output,
77
- ]
78
- )
79
-
80
- if __name__ == "__main__":
81
- interface.launch()
 
 
 
 
 
 
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()