joseluhf11 commited on
Commit
1fc91f5
1 Parent(s): 04a1ef7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importamos la librería
2
+ from youtube_transcript_api import YouTubeTranscriptApi
3
+ import re
4
+ from langchain.vectorstores import FAISS
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.embeddings import AwaEmbeddings
7
+ import os
8
+ import openai
9
+ import gradio as gr
10
+
11
+
12
+ def get_transcript(url):
13
+ video_id = re.search(r"(?<=v=)([^&#]+)", url)
14
+ video_id = video_id.group(0)
15
+
16
+ # retrieve the available transcripts
17
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
18
+
19
+ # iterate over all available transcripts
20
+ for transcript in transcript_list:
21
+ subtitles = transcript.translate('en').fetch()
22
+
23
+ # Imprimimos los transcript
24
+ text = ''
25
+ for sub in subtitles:
26
+ text = text + ' ' + sub['text']
27
+ return text
28
+
29
+
30
+ embeddings = AwaEmbeddings()
31
+
32
+ text_splitter = RecursiveCharacterTextSplitter(
33
+ # Set a really small chunk size, just to show.
34
+ chunk_size = 1500,
35
+ chunk_overlap = 100,
36
+ length_function = len,
37
+ is_separator_regex = False,
38
+ )
39
+
40
+ def chat(url, question, api_key):
41
+ os.environ["OPENAI_API_KEY"] = api_key
42
+ openai.api_key = api_key
43
+
44
+ info = get_transcript(url)
45
+ texts = text_splitter.create_documents([info])
46
+ db = FAISS.from_documents(texts, embeddings)
47
+ docs = db.similarity_search(question)
48
+
49
+ prompt = [
50
+ {"role": "system", "content": """You are my Youtube Asisstant. I will pass you texts from a Youtube Video Transcrip and I need you to use them to answer my question from the Youtube Video.
51
+ Please do not invent any information, and I am asking about information in the Youtube Video."""},
52
+ {"role":"user", "content": f"Context:{docs}"},
53
+ {"role":"user", "content": f"Question:{question}"},
54
+ ]
55
+ response = openai.ChatCompletion.create(
56
+ model="gpt-3.5-turbo-0613",
57
+ messages=prompt,
58
+ temperature = 0
59
+ )
60
+
61
+ return response["choices"][0]["message"]["content"]
62
+
63
+
64
+ # Create a gradio interface with two inputs and one output
65
+ demo = gr.Interface(
66
+ fn=chat, # The function to call
67
+ inputs=[gr.Textbox(label="Youtube URL"), gr.Textbox(label="Question"), gr.Textbox(label="API_KEY")], # The input components
68
+ outputs=gr.Textbox(label="Answer") # The output component
69
+ )
70
+
71
+ # Launch the interface
72
+ demo.launch(share=True)