|
import gradio as gr |
|
import os |
|
import json |
|
|
|
with open('results.json', 'r') as f: |
|
results_data = json.load(f) |
|
|
|
with open('sarcasm_data.json', 'r') as f: |
|
sarcasm_data = json.load(f) |
|
|
|
def get_dialogues_and_result(id): |
|
pred = next((item for item in results_data if item['id'] == id), None) |
|
if not pred: |
|
return f"ID {id} not found in results.json" |
|
|
|
sarcasm_info = sarcasm_data.get(id) |
|
if not sarcasm_info: |
|
return f"ID {id} not found in sarcasm_data.json" |
|
|
|
utterance = f"{sarcasm_info['speaker']}: {sarcasm_info['utterance']}" |
|
context = "\n".join( |
|
f"{speaker}: {line}" for speaker, line in zip(sarcasm_info['context_speakers'], sarcasm_info['context']) |
|
) |
|
|
|
output = { |
|
"id": id, |
|
"utterance": utterance, |
|
"context": context, |
|
"ground_truth": int(sarcasm_info['sarcasm']), |
|
"pred": pred['sarcasm'] |
|
} |
|
|
|
return output |
|
|
|
def analyze_videos(utterance_filename): |
|
utterance_id = os.path.splitext(os.path.basename(utterance_filename))[0] |
|
output = get_dialogues_and_result(utterance_id) |
|
|
|
return ( |
|
output["context"], |
|
output["utterance"], |
|
output["ground_truth"], |
|
output["pred"] |
|
) |
|
|
|
def custom_css(): |
|
return """ |
|
.gradio-heading { |
|
text-align: center; |
|
color: #0056b3; |
|
font-size: 28px; |
|
font-weight: bold; |
|
margin-bottom: 20px; |
|
} |
|
""" |
|
|
|
css_style = custom_css() |
|
|
|
with gr.Blocks(css=css_style) as interface: |
|
gr.Markdown("<div class='gradio-heading'>Sarcasm Detection</div>") |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
context_video_input = gr.Video(label="Upload Context Video") |
|
punchline_video_input = gr.Video(label="Upload Punchline Video") |
|
with gr.Column(scale=2): |
|
context_dialogue_output = gr.Textbox(label="Context Dialogues") |
|
punchline_dialogue_output = gr.Textbox(label="Punchline Dialogue") |
|
with gr.Column(): |
|
punchline_sarcasm_true = gr.Label(label="Ground Truth") |
|
punchline_sarcasm_pred = gr.Label(label="Sarcasm Detected") |
|
|
|
submit_button = gr.Button("Submit") |
|
|
|
submit_button.click( |
|
fn=analyze_videos, |
|
inputs=[punchline_video_input], |
|
outputs=[ |
|
context_dialogue_output, |
|
punchline_dialogue_output, |
|
punchline_sarcasm_true, |
|
punchline_sarcasm_pred |
|
] |
|
) |
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|