Spaces:
Runtime error
Runtime error
ravithejads
commited on
Commit
•
c044854
1
Parent(s):
5681bb1
Upload 2 files
Browse files- app.py +157 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
'audio_url': (None, 'http://files.gladia.io/example/audio-transcription/split_infinity.wav'),
|
42 |
+
'language': (None, 'english'),
|
43 |
+
'language_behaviour': (None, 'manual'),
|
44 |
+
'output_format': (None, 'json'),
|
45 |
+
}
|
46 |
+
|
47 |
+
response = requests.post(
|
48 |
+
'https://api.gladia.io/audio/text/audio-transcription/', headers=headers, files=files)
|
49 |
+
|
50 |
+
data = json.loads(response.text)
|
51 |
+
|
52 |
+
result = ""
|
53 |
+
for dict_ in data['prediction']:
|
54 |
+
result = result + dict_['transcription'] + " "
|
55 |
+
|
56 |
+
result = ' '.join(result.strip().split())
|
57 |
+
|
58 |
+
with open(f"{filename[:-4]}.txt", "w") as f:
|
59 |
+
f.write(result)
|
60 |
+
|
61 |
+
return result
|
62 |
+
|
63 |
+
|
64 |
+
def createindex(url, openaikey):
|
65 |
+
|
66 |
+
try:
|
67 |
+
filename = download_yt_video(url)
|
68 |
+
|
69 |
+
transcript = get_transcript(filename)
|
70 |
+
|
71 |
+
os.remove(filename)
|
72 |
+
|
73 |
+
# Store openai key in environment
|
74 |
+
os.environ['OPENAI_API_KEY'] = openaikey
|
75 |
+
|
76 |
+
# Create index
|
77 |
+
index = GPTListIndex([Document(transcript)], chunk_size_limit=2500)
|
78 |
+
|
79 |
+
index_filename = filename[:-4] + ".json"
|
80 |
+
index.save_to_disk(index_filename)
|
81 |
+
|
82 |
+
return "Video processed. Now you can start querying."
|
83 |
+
except Exception as e:
|
84 |
+
return e
|
85 |
+
|
86 |
+
|
87 |
+
def videoques(query, openaikey):
|
88 |
+
|
89 |
+
# Basic Checks
|
90 |
+
if not query:
|
91 |
+
return "Please enter your query."
|
92 |
+
|
93 |
+
# Basic Checks
|
94 |
+
if not openaikey:
|
95 |
+
return "Please enter openaikey."
|
96 |
+
|
97 |
+
# Store openai key in environment
|
98 |
+
os.environ['OPENAI_API_KEY'] = openaikey
|
99 |
+
|
100 |
+
index_name = "index.json"
|
101 |
+
|
102 |
+
index = GPTListIndex.load_from_disk(index_name)
|
103 |
+
|
104 |
+
# Query based on index
|
105 |
+
response = index.query(query, mode="embedding", similarity_top_k=4)
|
106 |
+
|
107 |
+
return response
|
108 |
+
|
109 |
+
|
110 |
+
def cleartext(query, output):
|
111 |
+
"""
|
112 |
+
Function to clear text
|
113 |
+
"""
|
114 |
+
return ["", ""]
|
115 |
+
|
116 |
+
|
117 |
+
with gr.Blocks() as demo:
|
118 |
+
gr.Markdown(
|
119 |
+
"""
|
120 |
+
<h1><center><b>Portuguese VideoQues</center></h1>
|
121 |
+
|
122 |
+
""")
|
123 |
+
gr.Markdown(
|
124 |
+
"""
|
125 |
+
Portuguese VideoQues answers your queries on any Portuguese video.
|
126 |
+
|
127 |
+
""")
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column():
|
130 |
+
url = gr.Textbox(lines=2, label="Enter Youtube Video link.")
|
131 |
+
openaikey = gr.Textbox(lines=2, label="Enter Your OpenAI key.")
|
132 |
+
submit1_button = gr.Button("Submit")
|
133 |
+
ans1_output = gr.Textbox(label="Status.")
|
134 |
+
clear1_button = gr.Button("Clear")
|
135 |
+
with gr.Column():
|
136 |
+
query = gr.Textbox(lines=2, label="Enter Your Query.")
|
137 |
+
submit2_button = gr.Button("Submit")
|
138 |
+
ans2_output = gr.Textbox(label="Answer.")
|
139 |
+
clear2_button = gr.Button("Clear")
|
140 |
+
|
141 |
+
# Submit button for showing YT Video thumbnail.
|
142 |
+
submit1_button.click(createindex, inputs=[
|
143 |
+
url, openaikey], outputs=[ans1_output])
|
144 |
+
|
145 |
+
# Submit button for submitting query.
|
146 |
+
submit2_button.click(videoques, inputs=[
|
147 |
+
query, openaikey], outputs=[ans2_output])
|
148 |
+
|
149 |
+
# Clear button for clearing query and answer.
|
150 |
+
clear1_button.click(cleartext, inputs=[
|
151 |
+
url, ans1_output], outputs=[url, ans1_output])
|
152 |
+
|
153 |
+
# Clear button for clearing query and answer.
|
154 |
+
clear2_button.click(cleartext, inputs=[query, ans2_output], outputs=[
|
155 |
+
query, ans2_output])
|
156 |
+
|
157 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
llama-index
|
3 |
+
pytube
|
4 |
+
pydub
|
5 |
+
openai
|