policy-video / app.py
themanas021's picture
Update app.py
d247bf3 verified
raw
history blame contribute delete
No virus
5.12 kB
from openai import OpenAI
from pydub import AudioSegment
import io
import gradio as gr
import os
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def extract_audio(video_file):
# Read video file into memory
video_data = video_file.read()
video_audio = AudioSegment.from_file(io.BytesIO(video_data), format="mp4")
audio_data = io.BytesIO()
video_audio.export(audio_data, format="mp3")
audio_data.seek(0)
return audio_data
def get_transcript(audio_file):
transcript = client.audio.transcriptions.create(
file=audio_file,
model="whisper-1",
response_format="verbose_json",
)
return transcript.text
def detect_keywords(transcript, query):
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant that can detect the important keywords from a paragraph"},
{"role": "user", "content": f'''{query}
Please get me those variable words given in the sample speech transcript.
The provided transcript to detect and map those are:
#only give me the mapping from above transcript to below transcript as:
"customer name": "manas",
scan for all variables present inside <> signs, if some mapping isn't done or mentioned in the below one. at last mention this is missing and rerecord the video
{transcript}'''}
]
)
return completion.choices[0].message.content
def process_video(video_file):
audio_file = extract_audio(video_file)
transcript = get_transcript(audio_file)
query = '''मैं <Customer Name>, एचडीएफ़सी लाइफ़ से <Policy Term> वर्षों के लिए एक स्टैंड-अलोन बीमा पॉलिसी <Product Name>, <Life Assured Name> के नाम से ले रहा/रही हूं, जिनका फ़ोन नंबर <Life Assured Mobile no> है, जिसके लिए मुझे <premium amount excluding taxes > रुपये लागू करों के साथ में <Payment Mode> आधार पर <Premium Payment Term> साल तक अदा करने हैं। मुझे पता है कि अगर मैं पूरे प्रीमियम भुगतान अवधि के लिए प्रीमियम का भुगतान नहीं करता/करती हूं तो मुझे पॉलिसी के पुरे लाभ नहीं मिल सकते है | मैं जानता/जानती हूं कि 5 साल पूरे होने के बाद निकासी की अनुमति दी जाती है| मुझे पॉलिसी के लाभों की जानकारी है, कृपया ऐप्लिकेशन के प्रोसेस के लिए इसे मेरी सहमति मानें। मुझे जानकारी है कि मैं उत्पाद के सभी नियमों और शर्तों के बारे में जानने के लिए पॉलिसी दस्तावेज का संदर्भ लें सकता/सकती हूँ तथा किसी भी विसंगति के लिए फ्री लुक अवधि के दौरान मैं बीमाकर्ता या बैंक से संपर्क कर सकता/सकती हूँ। मैं एतद्द्वारा पुष्टि करता/करती हूं कि इस बीमा पॉलिसी को खरीदते समय विक्रेता द्वारा किसी अन्य बैंकिंग उत्पादों या सेवाओं के लिए मुझसे कोई वादा नहीं किया गया था। मैं अपने बीमा प्रस्ताव के तेजी से प्रसंस्करण की सुविधा के लिए एचडीएफसी बैंक से अपनी व्यक्तिगत जानकारी, जैसे कि फोन नंबर, ईमेल आईडी, पता, गैर नकद क्रेडिट, प्रोफ़ाइल आदि, प्राप्त करने के लिए एचडीएफसी लाइफ को अपनी सहमति देता हूं।'''
result = detect_keywords(transcript, query)
return result
# Define the Gradio interface
iface = gr.Interface(
fn=process_video,
inputs=gr.File(label="Upload a Video"),
outputs="text",
title="Video to Keyword Extractor",
description="Upload a video file, and the app will extract audio, transcribe it, and detect important keywords based on a predefined query."
)
if __name__ == "__main__":
iface.launch()