File size: 4,444 Bytes
14610a8
 
 
 
 
 
 
c3c0dac
14610a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
980fa00
14610a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e737ee7
 
 
 
14610a8
e9a7f50
 
9eb9675
e737ee7
14610a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7839c76
 
b5612b3
 
7839c76
14610a8
 
b5612b3
7839c76
 
14610a8
7839c76
14610a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a54529
14610a8
0f0dccd
14610a8
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import speech_recognition as sr
from pydub import AudioSegment
import gradio as gr
from os import path
import requests
import openai
from openai import OpenAI
from moviepy.editor import *

prompt = "Type and press Enter"


def record_text(audio_file,api_key):
    client = OpenAI(api_key = api_key)
    input_file = audio_file
    output_file = "converted_sound.mp3"
    sound = AudioSegment.from_wav(input_file) 
    sound.export(output_file, format="mp3")
    audio_file = "converted_sound.mp3"
    audio_file = open(audio_file, "rb") 
    transcript = client.audio.transcriptions.create(
        model="whisper-1", 
        file=audio_file,
        response_format="srt"
        )
    return transcript
    # return(str(path.getsize(audio_file)/1000000)+'mb')
    # sound = audio_file
    # sound_type = sound.split(".")
    # if sound_type[-1] == 'mp3':
    #     input_file = sound
    #     output_file = "con_sound.wav"
        
    #     # convert mp3 file to wav file 
    #     sound = AudioSegment.from_mp3(input_file) 
    #     sound.export(output_file, format="wav")
    #     sound = "con_sound.wav"
    
    # MyText = ""
    # with sr.AudioFile(sound) as source:
    #     r.adjust_for_ambient_noise(source)
    #     print("Converting audio file to text..")
    #     audio2 = r.record(source, duration=None)  # Use record instead of listen
        
    #     MyText = r.recognize_google(audio2, language="en-US", key=None, show_all=False)
    #     MyText = MyText.lower()
    # return (MyText)


def api_calling(audio_file, prompt, api_key):
    audio_text = record_text(audio_file,api_key)
    sp_txt = audio_text.split("\n")
    new_lst = ''
    for i in range(2,len(sp_txt),4):
        new_lst = new_lst + ' ' + sp_txt[i]
    if len(prompt) == 0:
        prompt = '''Hi, act as a content writer and from the transcript provided to you separate all the text. 
        Apply proper punctuations, upper case and lower case to the provided text.'''
        
        return new_lst
    else:
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }
        payload = {
            "model": "gpt-3.5-turbo",
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": prompt
                        },
                        {
                            "type": "text",
                            "text": audio_text
                        }
                    ]
                }
            ],
            "max_tokens": 1000
        }
        response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
        audio_text_res = response.json()
        return audio_text_res["choices"][0]["message"]["content"]

def convert_to_mp3(mp4_file, wav_file):
    audio = wav_file
    video = VideoFileClip(mp4_file)
    audio = video.audio
    audio.write_audiofile(wav_file)

def message_and_history(audio_text,input, history, api_key):
    mp4_file = audio_text
    wav_file = "output.wav"
    convert_to_mp3(mp4_file, wav_file)
    history = history or []
    output_text = api_calling(wav_file,input,api_key)
    
    if len(input) == 0:
        input = "Speech from the video."
        history.append((input, output_text))
    else:
        history.append((input, output_text))
    
    return history, history


block = gr.Blocks(theme=gr.themes.Soft(primary_hue="slate"))
with block:
    gr.Markdown("""<h1><center>Stock-Analysis</center></h1> """)
    with gr.Row():
        with gr.Column(scale=0.5):
            vid_input = gr.Video(format="mp4", label="Upload .mp4 file")
            api_input = gr.Textbox(label="Enter Api-key")
            upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
        with gr.Column():
            chatbot = gr.Chatbot(label="Ask questions about the Video")
            message = gr.Textbox(label="User", placeholder=prompt)
            state = gr.State()
            
    upload_button.click(message_and_history, inputs=[vid_input,message, state, api_input], outputs=[chatbot, state])
    message.submit(message_and_history, inputs=[vid_input,message, state, api_input], outputs=[chatbot, state])
    message.submit(lambda: None, None, message, queue=False)
block.launch()