Spaces:
Sleeping
Sleeping
add .env
Browse files
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY="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"
|
main.py
DELETED
|
@@ -1,78 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
from langchain.document_loaders import YoutubeLoader
|
| 3 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain.llms import OpenAI
|
| 5 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
-
from langchain.prompts import PromptTemplate
|
| 7 |
-
from langchain.chains import LLMChain
|
| 8 |
-
from langchain.vectorstores import FAISS
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
import gradio as gr
|
| 11 |
-
from langchain.document_loaders import YoutubeLoader
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
load_dotenv()
|
| 15 |
-
|
| 16 |
-
embeddings = OpenAIEmbeddings()
|
| 17 |
-
|
| 18 |
-
# video_url = "https://www.youtube.com/watch?v=PfTOr3ONKzU"
|
| 19 |
-
def create_vector_db_from_youtube_url(video_url: str):
|
| 20 |
-
loader = YoutubeLoader.from_youtube_url(video_url)
|
| 21 |
-
transcript = loader.load()
|
| 22 |
-
|
| 23 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 24 |
-
docs = text_splitter.split_documents(transcript)
|
| 25 |
-
|
| 26 |
-
db = FAISS.from_documents(docs, embeddings)
|
| 27 |
-
return db
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# create_vector_db_from_youtube_url(video_url)
|
| 33 |
-
|
| 34 |
-
def get_response_from_query(db, query, k=4):
|
| 35 |
-
docs = db.similarity_search(query, k=k)
|
| 36 |
-
docs_page_content = " ".join([d.page_content for d in docs])
|
| 37 |
-
|
| 38 |
-
llm = OpenAI(model_name="text-davinci-003")
|
| 39 |
-
prompt = PromptTemplate(
|
| 40 |
-
input_variables=["question", "docs"],
|
| 41 |
-
template = """
|
| 42 |
-
Youare a helpful Youtube assistant that can answer questions about videos based on video transcript.
|
| 43 |
-
|
| 44 |
-
Answer the following question: {question}
|
| 45 |
-
By searching the following video transcript: {docs}
|
| 46 |
-
|
| 47 |
-
Only use the factua; information from the transcript to answer the question.
|
| 48 |
-
|
| 49 |
-
If you feel like you dont have enough information to answer the question, say "I dont know".
|
| 50 |
-
|
| 51 |
-
Your answer ahould be detailed.
|
| 52 |
-
"""
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
chain = LLMChain(llm=llm, prompt=prompt)
|
| 56 |
-
|
| 57 |
-
response = chain.run(question = query, docs = docs_page_content)
|
| 58 |
-
response = response.replace("\n", " ")
|
| 59 |
-
return response
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def gradio_interface(youtube_url, query):
|
| 63 |
-
if query and youtube_url:
|
| 64 |
-
db = create_vector_db_from_youtube_url(youtube_url)
|
| 65 |
-
response = get_response_from_query(db, query)
|
| 66 |
-
return response
|
| 67 |
-
|
| 68 |
-
# Membuat antarmuka Gradio
|
| 69 |
-
iface = gr.Interface(
|
| 70 |
-
fn=gradio_interface,
|
| 71 |
-
inputs=["text", "text"], # Dua input teks: URL YouTube dan pertanyaan
|
| 72 |
-
outputs="text", # Output berupa teks
|
| 73 |
-
title="YouTube Assistant",
|
| 74 |
-
description="Masukkan URL YouTube dan ajukan pertanyaan tentang video tersebut."
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
# Menjalankan antarmuka Gradio
|
| 78 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|