Spaces:
Runtime error
Runtime error
Commit ·
78b3700
1
Parent(s): 184c583
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,25 @@
|
|
| 3 |
import streamlit as st
|
| 4 |
import whisper
|
| 5 |
from pytube import YouTube
|
| 6 |
-
# from transformers import pipeline
|
| 7 |
import os
|
| 8 |
import openai
|
| 9 |
import pinecone
|
| 10 |
from sentence_transformers import SentenceTransformer
|
| 11 |
|
| 12 |
-
#
|
| 13 |
openai.api_key = "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"
|
| 14 |
|
| 15 |
-
# pinecode
|
| 16 |
pinecone.init(api_key="df54ffd1-2104-410e-b210-44f12f0c2dfd", environment="us-west1-gcp")
|
| 17 |
index = pinecone.Index("question-search")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
# load model
|
| 21 |
model = whisper.load_model("base")
|
| 22 |
-
# qa_model = pipeline("question-answering")
|
| 23 |
-
model_tf = SentenceTransformer('all-mpnet-base-v2')
|
| 24 |
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
def complete(prompt):
|
| 27 |
# query text-davinci-003
|
| 28 |
res = openai.Completion.create(
|
|
@@ -37,6 +36,7 @@ def complete(prompt):
|
|
| 37 |
)
|
| 38 |
return res['choices'][0]['text'].strip()
|
| 39 |
|
|
|
|
| 40 |
def query_context(prompt):
|
| 41 |
embeding_prompt = model_tf.encode(prompt)
|
| 42 |
embeding_prompt = embeding_prompt.tolist()
|
|
@@ -51,18 +51,19 @@ def query_context(prompt):
|
|
| 51 |
query_with_contexts = f"Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext: {resp['matches'][0]['metadata']['sentence']}\n\n---\n\nQuestion: {prompt}\nAnswer:"
|
| 52 |
return query_with_contexts
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.markdown("Upload Youtube URL")
|
| 57 |
-
|
| 58 |
-
# upload voice clip
|
| 59 |
audio_file = st.text_input("Upload an audio file")
|
|
|
|
| 60 |
# write a question
|
|
|
|
| 61 |
prompt = st.text_input("Wtite a question...")
|
| 62 |
|
| 63 |
# if button is submit
|
| 64 |
if st.button('Submit', key=1):
|
| 65 |
|
|
|
|
|
|
|
| 66 |
# grab the youtube video and name
|
| 67 |
youtube_video = YouTube(audio_file)
|
| 68 |
name = youtube_video.title
|
|
@@ -82,13 +83,10 @@ if st.button('Submit', key=1):
|
|
| 82 |
st.markdown("Transcription")
|
| 83 |
st.write(transcription)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
context = transcription
|
| 87 |
-
# st.write("Answer:")
|
| 88 |
-
# answer = qa_model(question = question, context = context)['answer']
|
| 89 |
-
# st.write(answer)
|
| 90 |
|
| 91 |
-
#
|
|
|
|
| 92 |
sentences = context.split(".")
|
| 93 |
|
| 94 |
# loop through sentences
|
|
@@ -104,9 +102,10 @@ if st.button('Submit', key=1):
|
|
| 104 |
i = i+1
|
| 105 |
|
| 106 |
|
|
|
|
| 107 |
full_query = query_context(prompt)
|
| 108 |
-
|
| 109 |
output = complete(full_query)
|
| 110 |
-
|
| 111 |
st.markdown("Answer")
|
| 112 |
st.write(output)
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import whisper
|
| 5 |
from pytube import YouTube
|
|
|
|
| 6 |
import os
|
| 7 |
import openai
|
| 8 |
import pinecone
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
|
| 11 |
+
# openAI setup
|
| 12 |
openai.api_key = "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"
|
| 13 |
|
| 14 |
+
# pinecode setup
|
| 15 |
pinecone.init(api_key="df54ffd1-2104-410e-b210-44f12f0c2dfd", environment="us-west1-gcp")
|
| 16 |
index = pinecone.Index("question-search")
|
| 17 |
|
| 18 |
+
# load model for transcription
|
|
|
|
| 19 |
model = whisper.load_model("base")
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# load model for embedding
|
| 22 |
+
model_tf = SentenceTransformer('all-mpnet-base-v2')
|
| 23 |
|
| 24 |
+
# function for complete prompt
|
| 25 |
def complete(prompt):
|
| 26 |
# query text-davinci-003
|
| 27 |
res = openai.Completion.create(
|
|
|
|
| 36 |
)
|
| 37 |
return res['choices'][0]['text'].strip()
|
| 38 |
|
| 39 |
+
# function to build context
|
| 40 |
def query_context(prompt):
|
| 41 |
embeding_prompt = model_tf.encode(prompt)
|
| 42 |
embeding_prompt = embeding_prompt.tolist()
|
|
|
|
| 51 |
query_with_contexts = f"Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext: {resp['matches'][0]['metadata']['sentence']}\n\n---\n\nQuestion: {prompt}\nAnswer:"
|
| 52 |
return query_with_contexts
|
| 53 |
|
| 54 |
+
# take video
|
| 55 |
+
st.markdown("Upload Youtube URL.........")
|
|
|
|
|
|
|
|
|
|
| 56 |
audio_file = st.text_input("Upload an audio file")
|
| 57 |
+
|
| 58 |
# write a question
|
| 59 |
+
st.markdown("Write Query ........")
|
| 60 |
prompt = st.text_input("Wtite a question...")
|
| 61 |
|
| 62 |
# if button is submit
|
| 63 |
if st.button('Submit', key=1):
|
| 64 |
|
| 65 |
+
###################### TRANSCRIPTION ############################
|
| 66 |
+
|
| 67 |
# grab the youtube video and name
|
| 68 |
youtube_video = YouTube(audio_file)
|
| 69 |
name = youtube_video.title
|
|
|
|
| 83 |
st.markdown("Transcription")
|
| 84 |
st.write(transcription)
|
| 85 |
|
| 86 |
+
###################### QA ############################
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# tokenize the context
|
| 89 |
+
context = transcription
|
| 90 |
sentences = context.split(".")
|
| 91 |
|
| 92 |
# loop through sentences
|
|
|
|
| 102 |
i = i+1
|
| 103 |
|
| 104 |
|
| 105 |
+
# build query with context
|
| 106 |
full_query = query_context(prompt)
|
| 107 |
+
# pass centexed query
|
| 108 |
output = complete(full_query)
|
| 109 |
+
# output answer
|
| 110 |
st.markdown("Answer")
|
| 111 |
st.write(output)
|