Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Commit 
							
							·
						
						ba5296b
	
1
								Parent(s):
							
							ff6a7f5
								
Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,54 +1,107 @@ 
     | 
|
| 1 | 
         
            -
            from  
     | 
| 2 | 
         
             
            import gradio as gr
         
     | 
| 3 | 
         
             
            import openai
         
     | 
| 4 | 
         
             
            import os
         
     | 
| 
         | 
|
| 5 | 
         | 
| 6 | 
         | 
| 7 | 
         
            -
            def  
     | 
| 8 | 
         
            -
             
     | 
| 9 | 
         
            -
                Function to check validity of openai key
         
     | 
| 10 | 
         
            -
                """
         
     | 
| 11 | 
         
            -
                # Set the API key
         
     | 
| 12 | 
         
            -
                openai.api_key = openaikey
         
     | 
| 13 | 
         
            -
                # Test the API key by making a request to the OpenAI API
         
     | 
| 14 | 
         
             
                try:
         
     | 
| 15 | 
         
            -
                    response = openai.Model.list()
         
     | 
| 16 | 
         
            -
                    return "Valid OpenAI API key"
         
     | 
| 17 | 
         
            -
                except openai.OpenAIError:
         
     | 
| 18 | 
         
            -
                    apikeylink = "https://beta.openai.com/account/api-keys"
         
     | 
| 19 | 
         
            -
                    return f"Incorrect OpenAI API key provided: {openaikey}. You can find your OpenAI API key here - {apikeylink}"
         
     | 
| 20 | 
         | 
| 
         | 
|
| 21 | 
         | 
| 22 | 
         
            -
             
     | 
| 23 | 
         
            -
             
     | 
| 24 | 
         
            -
             
     | 
| 25 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 26 | 
         | 
| 27 | 
         
             
                # Basic Checks
         
     | 
| 28 | 
         
            -
                if not index_type:
         
     | 
| 29 | 
         
            -
                    return "Please Select source type"
         
     | 
| 30 | 
         
             
                if not query:
         
     | 
| 31 | 
         
             
                    return "Please enter your query."
         
     | 
| 32 | 
         
            -
                if not apikey:
         
     | 
| 33 | 
         
            -
                    return "Please enter your openai apikey."
         
     | 
| 34 | 
         
            -
             
     | 
| 35 | 
         
            -
                openaiapikeyvality = openaiapikeyvaliditycheck(apikey)
         
     | 
| 36 | 
         | 
| 37 | 
         
            -
                 
     | 
| 38 | 
         
            -
             
     | 
| 
         | 
|
| 39 | 
         | 
| 40 | 
         
             
                # Store openai key in environment
         
     | 
| 41 | 
         
            -
                os.environ['OPENAI_API_KEY'] =  
     | 
| 42 | 
         | 
| 43 | 
         
            -
                 
     | 
| 44 | 
         | 
| 45 | 
         
            -
                 
     | 
| 46 | 
         
            -
                    index = GPTListIndex.load_from_disk('index_audiolong.json')
         
     | 
| 47 | 
         
            -
                elif index_type == "Conheça a Robotizia - Criação de conteúdos com Inteligência Artificial, uma startup da ReportFlex":
         
     | 
| 48 | 
         
            -
                    index = GPTListIndex.load_from_disk('index_audioshort.json')
         
     | 
| 49 | 
         | 
| 50 | 
         
             
                # Query based on index
         
     | 
| 51 | 
         
            -
                response = index.query(query,  
     | 
| 52 | 
         | 
| 53 | 
         
             
                return response
         
     | 
| 54 | 
         | 
| 
         @@ -65,24 +118,39 @@ with gr.Blocks() as demo: 
     | 
|
| 65 | 
         
             
                    """
         
     | 
| 66 | 
         
             
                <h1><center><b>VideoQues</center></h1>
         
     | 
| 67 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 68 | 
         
             
                """)
         
     | 
| 69 | 
         
             
                with gr.Row():
         
     | 
| 70 | 
         
             
                    with gr.Column():
         
     | 
| 71 | 
         
            -
                         
     | 
| 72 | 
         
            -
             
     | 
| 73 | 
         
            -
                         
     | 
| 74 | 
         
            -
                         
     | 
| 75 | 
         
            -
                         
     | 
| 76 | 
         
             
                    with gr.Column():
         
     | 
| 77 | 
         
            -
                         
     | 
| 78 | 
         
            -
                         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 79 | 
         | 
| 80 | 
         
             
                # Submit button for submitting query.
         
     | 
| 81 | 
         
            -
                 
     | 
| 82 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 83 | 
         | 
| 84 | 
         
             
                # Clear button for clearing query and answer.
         
     | 
| 85 | 
         
            -
                 
     | 
| 86 | 
         
            -
             
     | 
| 87 | 
         | 
| 88 | 
         
            -
            demo.launch(debug=True)
         
     | 
| 
         | 
|
| 1 | 
         
            +
            from llama_index import Document, GPTListIndex, GPTSimpleVectorIndex
         
     | 
| 2 | 
         
             
            import gradio as gr
         
     | 
| 3 | 
         
             
            import openai
         
     | 
| 4 | 
         
             
            import os
         
     | 
| 5 | 
         
            +
            from pytube import YouTube
         
     | 
| 6 | 
         | 
| 7 | 
         | 
| 8 | 
         
            +
            def download_yt_video(ytlink):
         
     | 
| 9 | 
         
            +
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 10 | 
         
             
                try:
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 11 | 
         | 
| 12 | 
         
            +
                    yt = YouTube(ytlink)
         
     | 
| 13 | 
         | 
| 14 | 
         
            +
                    video = yt.streams.filter(only_audio=True).first()
         
     | 
| 15 | 
         
            +
             
     | 
| 16 | 
         
            +
                    out_file = video.download(output_path="./")
         
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
                    base, ext = os.path.splitext(out_file)
         
     | 
| 19 | 
         
            +
                    new_file = base + '.mp3'
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
                    os.rename(out_file, new_file)
         
     | 
| 22 | 
         
            +
             
     | 
| 23 | 
         
            +
                    return new_file
         
     | 
| 24 | 
         
            +
                except Exception as e:
         
     | 
| 25 | 
         
            +
                    return e
         
     | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
            +
             
     | 
| 28 | 
         
            +
            def get_transcript(filename):
         
     | 
| 29 | 
         
            +
                import requests
         
     | 
| 30 | 
         
            +
                import json
         
     | 
| 31 | 
         
            +
             
     | 
| 32 | 
         
            +
                headers = {
         
     | 
| 33 | 
         
            +
                    'accept': 'application/json',
         
     | 
| 34 | 
         
            +
                    'x-gladia-key': '70ad5f6e-31e6-4acf-8a15-89c166c4cc9f',
         
     | 
| 35 | 
         
            +
                    # requests won't add a boundary if this header is set when you pass files=
         
     | 
| 36 | 
         
            +
                    # 'Content-Type': 'multipart/form-data',
         
     | 
| 37 | 
         
            +
                }
         
     | 
| 38 | 
         
            +
             
     | 
| 39 | 
         
            +
                files = {
         
     | 
| 40 | 
         
            +
                    'audio': (filename, open(filename, 'rb'), 'audio/mpeg'),
         
     | 
| 41 | 
         
            +
                    'language': (None, 'english'),
         
     | 
| 42 | 
         
            +
                    'language_behaviour': (None, 'manual'),
         
     | 
| 43 | 
         
            +
                    'output_format': (None, 'json'),
         
     | 
| 44 | 
         
            +
                }
         
     | 
| 45 | 
         
            +
             
     | 
| 46 | 
         
            +
                response = requests.post(
         
     | 
| 47 | 
         
            +
                    'https://api.gladia.io/audio/text/audio-transcription/', headers=headers, files=files)
         
     | 
| 48 | 
         
            +
             
     | 
| 49 | 
         
            +
                data = json.loads(response.text)
         
     | 
| 50 | 
         
            +
             
     | 
| 51 | 
         
            +
                result = ""
         
     | 
| 52 | 
         
            +
                for dict_ in data['prediction']:
         
     | 
| 53 | 
         
            +
                    result = result + dict_['transcription'] + " "
         
     | 
| 54 | 
         
            +
             
     | 
| 55 | 
         
            +
                result = ' '.join(result.strip().split())
         
     | 
| 56 | 
         
            +
             
     | 
| 57 | 
         
            +
                with open(f"{filename[:-4]}.txt", "w") as f:
         
     | 
| 58 | 
         
            +
                    f.write(result)
         
     | 
| 59 | 
         
            +
             
     | 
| 60 | 
         
            +
                return result
         
     | 
| 61 | 
         
            +
             
     | 
| 62 | 
         
            +
             
     | 
| 63 | 
         
            +
            def createindex(url, openaikey):
         
     | 
| 64 | 
         
            +
             
     | 
| 65 | 
         
            +
                try:
         
     | 
| 66 | 
         
            +
                    filename = download_yt_video(url)
         
     | 
| 67 | 
         
            +
             
     | 
| 68 | 
         
            +
                    transcript = get_transcript(filename)
         
     | 
| 69 | 
         
            +
             
     | 
| 70 | 
         
            +
                    os.remove(filename)
         
     | 
| 71 | 
         
            +
             
     | 
| 72 | 
         
            +
                    # Store openai key in environment
         
     | 
| 73 | 
         
            +
                    os.environ['OPENAI_API_KEY'] = openaikey
         
     | 
| 74 | 
         
            +
             
     | 
| 75 | 
         
            +
                    # Create index
         
     | 
| 76 | 
         
            +
                    index = GPTListIndex([Document(transcript)], chunk_size_limit=2500)
         
     | 
| 77 | 
         
            +
             
     | 
| 78 | 
         
            +
                    index_filename = "index.json"
         
     | 
| 79 | 
         
            +
                    index.save_to_disk(index_filename)
         
     | 
| 80 | 
         
            +
             
     | 
| 81 | 
         
            +
                    return "Video processed. Now you can start querying."
         
     | 
| 82 | 
         
            +
                except Exception as e:
         
     | 
| 83 | 
         
            +
                    return e
         
     | 
| 84 | 
         
            +
             
     | 
| 85 | 
         
            +
             
     | 
| 86 | 
         
            +
            def videoques(query, openaikey):
         
     | 
| 87 | 
         | 
| 88 | 
         
             
                # Basic Checks
         
     | 
| 
         | 
|
| 
         | 
|
| 89 | 
         
             
                if not query:
         
     | 
| 90 | 
         
             
                    return "Please enter your query."
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 91 | 
         | 
| 92 | 
         
            +
                # Basic Checks
         
     | 
| 93 | 
         
            +
                if not openaikey:
         
     | 
| 94 | 
         
            +
                    return "Please enter openaikey."
         
     | 
| 95 | 
         | 
| 96 | 
         
             
                # Store openai key in environment
         
     | 
| 97 | 
         
            +
                os.environ['OPENAI_API_KEY'] = openaikey
         
     | 
| 98 | 
         | 
| 99 | 
         
            +
                index_name = "index.json"
         
     | 
| 100 | 
         | 
| 101 | 
         
            +
                index = GPTListIndex.load_from_disk(index_name)
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 102 | 
         | 
| 103 | 
         
             
                # Query based on index
         
     | 
| 104 | 
         
            +
                response = index.query(query, mode="embedding", similarity_top_k=4)
         
     | 
| 105 | 
         | 
| 106 | 
         
             
                return response
         
     | 
| 107 | 
         | 
| 
         | 
|
| 118 | 
         
             
                    """
         
     | 
| 119 | 
         
             
                <h1><center><b>VideoQues</center></h1>
         
     | 
| 120 | 
         | 
| 121 | 
         
            +
                """)
         
     | 
| 122 | 
         
            +
                gr.Markdown(
         
     | 
| 123 | 
         
            +
                    """
         
     | 
| 124 | 
         
            +
                VideoQues answers your queries on any youtube video.
         
     | 
| 125 | 
         
            +
                
         
     | 
| 126 | 
         
             
                """)
         
     | 
| 127 | 
         
             
                with gr.Row():
         
     | 
| 128 | 
         
             
                    with gr.Column():
         
     | 
| 129 | 
         
            +
                        url = gr.Textbox(lines=1, label="Enter Youtube Video link.")
         
     | 
| 130 | 
         
            +
                        openaikey = gr.Textbox(lines=1, label="Enter Your OpenAI key.")
         
     | 
| 131 | 
         
            +
                        submit1_button = gr.Button("Submit")
         
     | 
| 132 | 
         
            +
                        ans1_output = gr.Textbox(label="Status.")
         
     | 
| 133 | 
         
            +
                        clear1_button = gr.Button("Clear")
         
     | 
| 134 | 
         
             
                    with gr.Column():
         
     | 
| 135 | 
         
            +
                        query = gr.Textbox(lines=2, label="Enter Your Query.")
         
     | 
| 136 | 
         
            +
                        submit2_button = gr.Button("Submit")
         
     | 
| 137 | 
         
            +
                        ans2_output = gr.Textbox(label="Answer.")
         
     | 
| 138 | 
         
            +
                        clear2_button = gr.Button("Clear")
         
     | 
| 139 | 
         
            +
             
     | 
| 140 | 
         
            +
                # Submit button for showing YT Video thumbnail.
         
     | 
| 141 | 
         
            +
                submit1_button.click(createindex, inputs=[
         
     | 
| 142 | 
         
            +
                                     url, openaikey], outputs=[ans1_output])
         
     | 
| 143 | 
         | 
| 144 | 
         
             
                # Submit button for submitting query.
         
     | 
| 145 | 
         
            +
                submit2_button.click(videoques, inputs=[
         
     | 
| 146 | 
         
            +
                                     query, openaikey], outputs=[ans2_output])
         
     | 
| 147 | 
         
            +
             
     | 
| 148 | 
         
            +
                # Clear button for clearing query and answer.
         
     | 
| 149 | 
         
            +
                clear1_button.click(cleartext, inputs=[
         
     | 
| 150 | 
         
            +
                                    url, ans1_output], outputs=[url, ans1_output])
         
     | 
| 151 | 
         | 
| 152 | 
         
             
                # Clear button for clearing query and answer.
         
     | 
| 153 | 
         
            +
                clear2_button.click(cleartext, inputs=[query, ans2_output], outputs=[
         
     | 
| 154 | 
         
            +
                                    query, ans2_output])
         
     | 
| 155 | 
         | 
| 156 | 
         
            +
            demo.launch(debug=True)
         
     |