File size: 4,082 Bytes
babad83
9f1f65f
babad83
 
 
707bcd0
6453a05
 
babad83
 
 
 
f64491c
 
 
babad83
 
 
 
707bcd0
f64491c
 
 
 
 
 
 
babad83
 
 
 
 
 
 
 
 
23744a2
 
 
 
6453a05
 
23744a2
597c937
babad83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6453a05
babad83
 
6453a05
 
 
 
 
babad83
 
 
 
 
 
597c937
babad83
 
597c937
 
 
 
 
 
babad83
 
 
 
 
 
 
 
 
 
 
 
597c937
babad83
 
 
 
 
 
 
 
 
 
 
 
de52432
babad83
 
 
 
 
 
 
23744a2
 
babad83
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import gradio as gr
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_core.documents import Document
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_community.document_loaders import YoutubeLoader
from langchain_community.document_loaders import GoogleApiYoutubeLoader
import tiktoken
import os
from dotenv import load_dotenv
import json
from groq import Groq
from pydantic import BaseModel
from typing import List 

# Load environment variables
load_dotenv()
groq_api_key = os.getenv("GROQ_API_KEY")
os.environ["USER_AGENT"] = "RAG-chat-app"

client = Groq(api_key=groq_api_key)

primer = f"""You are a personal assistant. Answer any questions I have about the Youtube Video provided.
Translate in specific language if user asks you to
"""

# Initialize Hugging Face embeddings
hf_embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# Initialize ChromaDB vector store
vector_store = Chroma(
    collection_name="data_collection",
    embedding_function=hf_embeddings,
)

# # Load and process YouTube video
# loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=e-gwvmhyU7A", add_video_info=True)
# data = loader.load()  # Assume this loads the transcript

loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=e-gwvmhyU7A", add_video_info=True)
data = loader.load()



tokenizer = tiktoken.get_encoding('p50k_base')

def tiktoken_len(text):
    tokens = tokenizer.encode(
        text,
        disallowed_special=()
    )
    return len(tokens)

# Initialize text splitter
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=2000,
    chunk_overlap=100,
    length_function=tiktoken_len,
    separators=["\n\n", "\n", " ", ""]
)

# Split data from YouTube video
texts = text_splitter.split_documents(data)

# Store documents in ChromaDB
documents= [
    Document(
        page_content=f"Source: {t.metadata['source']}, Title: {t.metadata['title']} \n\nContent: {t.page_content}",
                   metadata=t.metadata
                   )
    for t in texts]

vectorstore_from_texts = vector_store.add_documents(documents=documents)

# Define function to get embeddings from Hugging Face
def get_embedding(text):
    return hf_embeddings.embed_query(text)

# Define Gradio interface function
def query_model(messages):
    try:
        # Call the function for user query vector embeddings
        if isinstance(messages, list) and len(messages) > 0:
            latest_message = messages[-1]['content']
        else:
            return "No messages provided or invalid format."    

        raw_query_embedding= get_embedding(latest_message)
        # Perform similarity search with vector store
        results = vector_store.similarity_search_by_vector(
            embedding=raw_query_embedding, k=1
        )
        
        contexts = [doc.page_content for doc in results]
        
        # Prepare context for RAG
        augmented_query = (
            "<CONTEXT>\n" +
            "\n\n-------\n\n".join(contexts) +
            "\n-------\n</CONTEXT>\n\n\n\nMY QUESTION:\n" +
            messages
        )
        
        # Call to Groq or Hugging Face model for completion
        response = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=[
            {"role": "system", "content": primer},
            {"role": "user", "content": augmented_query},
            ],
            max_tokens=1000,
            temperature=1.2)
        
        return {'assistantMessage':response.choices[0].message.content}
    
    except Exception as e:
        return str(e)

# Create Gradio interface
iface = gr.Interface(
    fn=query_model,
    inputs=gr.JSON(label="Enter array of messages (JSON format)"),
    outputs=gr.Textbox(label="Response"),
    title="RAG Model",
    description="Retrieve and Generate responses from a YouTube video transcript."
)

if __name__ == "__main__":
    iface.launch()