Hanzo03 commited on
Commit
1b3e1f1
·
1 Parent(s): e83ac08

initial commit

Browse files
Files changed (4) hide show
  1. .env +1 -1
  2. .python-version +1 -1
  3. modules/rag_query.py +25 -33
  4. uv.lock +20 -3
.env CHANGED
@@ -1 +1 @@
1
- GEMINI_API_KEY = "your_gemini_api_key_here"
 
1
+ HUGGINGFACEHUB_API_TOKEN = "your_huggingface_api_key_here"
.python-version CHANGED
@@ -1 +1 @@
1
- 3.10
 
1
+ 3.12
modules/rag_query.py CHANGED
@@ -1,51 +1,48 @@
 
1
  import chromadb
2
- from langchain_google_genai import ChatGoogleGenerativeAI
3
  from langchain_core.prompts import ChatPromptTemplate
4
  from langchain_core.runnables import RunnablePassthrough
5
  from langchain_huggingface.embeddings import HuggingFaceEmbeddings
6
  from langchain_chroma import Chroma
7
- import os
8
-
9
- from dotenv import load_dotenv
10
-
11
- load_dotenv() # Load environment variables from .env file
12
 
13
  # 1. Initialize RAG Components
14
- GEMINI_MODEL = "gemini-2.5-flash"
15
- COLLECTION_NAME = 'video_analysis_data' # Updated collection name
16
  DB_PATH = "./chroma_db"
17
 
18
  def run_query(user_query):
19
- """
20
- Executes the RAG pipeline: Retrieve relevant context and generate a general answer.
21
- """
22
- if not os.path.exists(DB_PATH):
23
- print(f"Error: Database path {DB_PATH} not found. Run 'rag_indexer.py' first.")
24
- return "Analysis data is not yet indexed. Please index the data first."
25
 
26
- # ChromaDB setup
27
  client = chromadb.PersistentClient(path=DB_PATH)
28
-
29
  embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
 
30
  vectorstore = Chroma(
31
  client=client,
32
- collection_name=COLLECTION_NAME,
33
  embedding_function=embedding_function
34
  )
35
 
36
- # 2. Retrieval (R)
37
- retriever = vectorstore.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 relevant documents
38
 
39
- # 3. Generation (G)
40
- # The new general-purpose system prompt
41
- template = """
42
- You are an expert Video Content Analyst. Your task is to describe the scene, objects, and potential context detected in a video based on the provided analysis logs (Context).
43
- Use the Context to answer the user's Question.
44
-
45
- IMPORTANT: Since the analysis only provides object labels and locations (Object Detection) and not specific actions (Activity Recognition), you must infer and describe the *scene* or *context* based on the objects present (e.g., "The presence of people and construction materials suggests activity on a construction site.").
 
46
 
47
- Always include the video timestamp(s) where the relevant context was found.
 
48
 
 
 
 
 
 
49
  Context:
50
  {context}
51
 
@@ -53,19 +50,14 @@ def run_query(user_query):
53
  """
54
  prompt = ChatPromptTemplate.from_template(template)
55
 
56
- llm = ChatGoogleGenerativeAI(model=GEMINI_MODEL)
57
-
58
- # The RAG Chain logic
59
  rag_chain = (
60
  {"context": retriever, "question": RunnablePassthrough()}
61
  | prompt
62
  | llm
63
  )
64
 
65
- # 4. Execute the chain
66
- print(f"Executing RAG query for: '{user_query}'...")
67
  response = rag_chain.invoke(user_query)
68
-
69
  return response.content
70
 
71
  # Example Usage:
 
1
+ import os
2
  import chromadb
3
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
4
  from langchain_core.prompts import ChatPromptTemplate
5
  from langchain_core.runnables import RunnablePassthrough
6
  from langchain_huggingface.embeddings import HuggingFaceEmbeddings
7
  from langchain_chroma import Chroma
 
 
 
 
 
8
 
9
  # 1. Initialize RAG Components
10
+ REPO_ID = "Qwen/Qwen2.5-7B-Instruct"
11
+ COLLECTION_NAME = 'video_analysis_data'
12
  DB_PATH = "./chroma_db"
13
 
14
  def run_query(user_query):
15
+ if not os.getenv("HUGGINGFACEHUB_API_TOKEN"):
16
+ return "Error: Please set HUGGINGFACEHUB_API_TOKEN in your environment variables."
 
 
 
 
17
 
 
18
  client = chromadb.PersistentClient(path=DB_PATH)
 
19
  embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
20
+
21
  vectorstore = Chroma(
22
  client=client,
23
+ collection_name=COLLECTION_NAME,
24
  embedding_function=embedding_function
25
  )
26
 
27
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
 
28
 
29
+ # 2. LLM Setup (Qwen via Hugging Face Endpoint)
30
+ llm_endpoint = HuggingFaceEndpoint(
31
+ repo_id=REPO_ID,
32
+ task="text-generation",
33
+ max_new_tokens=512,
34
+ repetition_penalty=1.1,
35
+ huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
36
+ )
37
 
38
+ # Wrap it in ChatHuggingFace to handle the prompt templates correctly
39
+ llm = ChatHuggingFace(llm=llm_endpoint)
40
 
41
+ template = """
42
+ You are an expert Video Content Analyst. Use the Context to answer the Question.
43
+ If you don't know the answer, say you don't know.
44
+ Infer activity based on detected objects (e.g., people + skateboards = skateboarding).
45
+
46
  Context:
47
  {context}
48
 
 
50
  """
51
  prompt = ChatPromptTemplate.from_template(template)
52
 
 
 
 
53
  rag_chain = (
54
  {"context": retriever, "question": RunnablePassthrough()}
55
  | prompt
56
  | llm
57
  )
58
 
 
 
59
  response = rag_chain.invoke(user_query)
60
+ # Hugging Face responses sometimes need a little cleaning depending on the version
61
  return response.content
62
 
63
  # Example Usage:
uv.lock CHANGED
@@ -2211,6 +2211,23 @@ wheels = [
2211
  { url = "https://files.pythonhosted.org/packages/a4/7d/f1c30a92854540bf789e9cd5dde7ef49bbe63f855b85a2e6b3db8135c591/opencv_python-4.11.0.86-cp37-abi3-win_amd64.whl", hash = "sha256:085ad9b77c18853ea66283e98affefe2de8cc4c1f43eda4c100cf9b2721142ec", size = 39488044, upload-time = "2025-01-16T13:52:21.928Z" },
2212
  ]
2213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2214
  [[package]]
2215
  name = "opentelemetry-api"
2216
  version = "1.39.1"
@@ -3132,7 +3149,7 @@ dependencies = [
3132
  { name = "langchain-google-genai" },
3133
  { name = "langchain-huggingface" },
3134
  { name = "numpy" },
3135
- { name = "opencv-python" },
3136
  { name = "python-dotenv" },
3137
  { name = "sentence-transformers" },
3138
  { name = "streamlit" },
@@ -3151,8 +3168,8 @@ requires-dist = [
3151
  { name = "langchain-core", specifier = ">=1.1.3" },
3152
  { name = "langchain-google-genai", specifier = ">=4.0.0" },
3153
  { name = "langchain-huggingface", specifier = ">=1.1.0" },
3154
- { name = "numpy", specifier = ">=2.3.5" },
3155
- { name = "opencv-python", specifier = ">=4.11.0.86" },
3156
  { name = "python-dotenv", specifier = ">=1.2.1" },
3157
  { name = "sentence-transformers", specifier = ">=5.2.0" },
3158
  { name = "streamlit", specifier = ">=1.52.1" },
 
2211
  { url = "https://files.pythonhosted.org/packages/a4/7d/f1c30a92854540bf789e9cd5dde7ef49bbe63f855b85a2e6b3db8135c591/opencv_python-4.11.0.86-cp37-abi3-win_amd64.whl", hash = "sha256:085ad9b77c18853ea66283e98affefe2de8cc4c1f43eda4c100cf9b2721142ec", size = 39488044, upload-time = "2025-01-16T13:52:21.928Z" },
2212
  ]
2213
 
2214
+ [[package]]
2215
+ name = "opencv-python-headless"
2216
+ version = "4.11.0.86"
2217
+ source = { registry = "https://pypi.org/simple" }
2218
+ dependencies = [
2219
+ { name = "numpy" },
2220
+ ]
2221
+ sdist = { url = "https://files.pythonhosted.org/packages/36/2f/5b2b3ba52c864848885ba988f24b7f105052f68da9ab0e693cc7c25b0b30/opencv-python-headless-4.11.0.86.tar.gz", hash = "sha256:996eb282ca4b43ec6a3972414de0e2331f5d9cda2b41091a49739c19fb843798", size = 95177929, upload-time = "2025-01-16T13:53:40.22Z" }
2222
+ wheels = [
2223
+ { url = "https://files.pythonhosted.org/packages/dc/53/2c50afa0b1e05ecdb4603818e85f7d174e683d874ef63a6abe3ac92220c8/opencv_python_headless-4.11.0.86-cp37-abi3-macosx_13_0_arm64.whl", hash = "sha256:48128188ade4a7e517237c8e1e11a9cdf5c282761473383e77beb875bb1e61ca", size = 37326460, upload-time = "2025-01-16T13:52:57.015Z" },
2224
+ { url = "https://files.pythonhosted.org/packages/3b/43/68555327df94bb9b59a1fd645f63fafb0762515344d2046698762fc19d58/opencv_python_headless-4.11.0.86-cp37-abi3-macosx_13_0_x86_64.whl", hash = "sha256:a66c1b286a9de872c343ee7c3553b084244299714ebb50fbdcd76f07ebbe6c81", size = 56723330, upload-time = "2025-01-16T13:55:45.731Z" },
2225
+ { url = "https://files.pythonhosted.org/packages/45/be/1438ce43ebe65317344a87e4b150865c5585f4c0db880a34cdae5ac46881/opencv_python_headless-4.11.0.86-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6efabcaa9df731f29e5ea9051776715b1bdd1845d7c9530065c7951d2a2899eb", size = 29487060, upload-time = "2025-01-16T13:51:59.625Z" },
2226
+ { url = "https://files.pythonhosted.org/packages/dd/5c/c139a7876099916879609372bfa513b7f1257f7f1a908b0bdc1c2328241b/opencv_python_headless-4.11.0.86-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0e0a27c19dd1f40ddff94976cfe43066fbbe9dfbb2ec1907d66c19caef42a57b", size = 49969856, upload-time = "2025-01-16T13:53:29.654Z" },
2227
+ { url = "https://files.pythonhosted.org/packages/95/dd/ed1191c9dc91abcc9f752b499b7928aacabf10567bb2c2535944d848af18/opencv_python_headless-4.11.0.86-cp37-abi3-win32.whl", hash = "sha256:f447d8acbb0b6f2808da71fddd29c1cdd448d2bc98f72d9bb78a7a898fc9621b", size = 29324425, upload-time = "2025-01-16T13:52:49.048Z" },
2228
+ { url = "https://files.pythonhosted.org/packages/86/8a/69176a64335aed183529207ba8bc3d329c2999d852b4f3818027203f50e6/opencv_python_headless-4.11.0.86-cp37-abi3-win_amd64.whl", hash = "sha256:6c304df9caa7a6a5710b91709dd4786bf20a74d57672b3c31f7033cc638174ca", size = 39402386, upload-time = "2025-01-16T13:52:56.418Z" },
2229
+ ]
2230
+
2231
  [[package]]
2232
  name = "opentelemetry-api"
2233
  version = "1.39.1"
 
3149
  { name = "langchain-google-genai" },
3150
  { name = "langchain-huggingface" },
3151
  { name = "numpy" },
3152
+ { name = "opencv-python-headless" },
3153
  { name = "python-dotenv" },
3154
  { name = "sentence-transformers" },
3155
  { name = "streamlit" },
 
3168
  { name = "langchain-core", specifier = ">=1.1.3" },
3169
  { name = "langchain-google-genai", specifier = ">=4.0.0" },
3170
  { name = "langchain-huggingface", specifier = ">=1.1.0" },
3171
+ { name = "numpy", specifier = "==2.3.5" },
3172
+ { name = "opencv-python-headless", specifier = ">=4.10.0.84" },
3173
  { name = "python-dotenv", specifier = ">=1.2.1" },
3174
  { name = "sentence-transformers", specifier = ">=5.2.0" },
3175
  { name = "streamlit", specifier = ">=1.52.1" },