ashishanand
commited on
Commit
•
d7955f6
1
Parent(s):
d191906
Initial commit
Browse files- app.py +148 -0
- chromadb/chroma.sqlite3 +3 -0
- chromadb/e820442b-1d6c-4933-8a2c-981f60377458/data_level0.bin +3 -0
- chromadb/e820442b-1d6c-4933-8a2c-981f60377458/header.bin +3 -0
- chromadb/e820442b-1d6c-4933-8a2c-981f60377458/length.bin +3 -0
- chromadb/e820442b-1d6c-4933-8a2c-981f60377458/link_lists.bin +0 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import numpy as np
|
6 |
+
import torch
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
import pdfplumber
|
9 |
+
from chromadb import Client
|
10 |
+
from chromadb.config import Settings
|
11 |
+
from chromadb.utils import embedding_functions
|
12 |
+
from transformers import AutoTokenizer
|
13 |
+
from rerankers import Reranker
|
14 |
+
from transformers import GPT2TokenizerFast
|
15 |
+
from groq import Groq
|
16 |
+
|
17 |
+
import gradio as gr
|
18 |
+
|
19 |
+
# Retrieve the API key from environment variables (Hugging Face Secrets)
|
20 |
+
groq_api_key = os.environ.get('GROQ_API_KEY')
|
21 |
+
|
22 |
+
# Initialize the chat client with the API key
|
23 |
+
chat_client = Groq(api_key=groq_api_key)
|
24 |
+
model = "llama-3.2-90b-text-preview"
|
25 |
+
|
26 |
+
# Define your functions (same as before)
|
27 |
+
def preprocess_text(text):
|
28 |
+
# ... (same as your original function)
|
29 |
+
text = re.sub(r'\s+', ' ', text)
|
30 |
+
text = text.strip()
|
31 |
+
return text
|
32 |
+
|
33 |
+
def call_Llama_api(query, context):
|
34 |
+
# ... (same as your original function)
|
35 |
+
chat_completion = chat_client.chat.completions.create(
|
36 |
+
messages=[
|
37 |
+
{
|
38 |
+
"role": "system",
|
39 |
+
"content": "You are a car technician. Given the user's question and relevant excerpts from different car manuals, answer the question by including direct quotes from the correct car manual. Be concise and to the point in your response."
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"role": "user",
|
43 |
+
"content": "User Question: " + query + "\n\nRelevant Excerpt(s):\n\n" + context,
|
44 |
+
}
|
45 |
+
],
|
46 |
+
temperature=0.7,
|
47 |
+
max_tokens=50,
|
48 |
+
top_p=1,
|
49 |
+
stream=False,
|
50 |
+
stop=None,
|
51 |
+
model=model
|
52 |
+
)
|
53 |
+
response = chat_completion.choices[0].message.content
|
54 |
+
return response
|
55 |
+
|
56 |
+
def is_car_model_available(query, available_models):
|
57 |
+
# ... (same as your original function)
|
58 |
+
for model in available_models:
|
59 |
+
if model.lower() in query.lower():
|
60 |
+
return model
|
61 |
+
return None
|
62 |
+
|
63 |
+
def colbert_rerank(query=None, chunks=None):
|
64 |
+
# ... (same as your original function)
|
65 |
+
d = ranker.rank(query=query, docs=chunks)
|
66 |
+
reranked_chunks = [d[i].text for i in range(len(chunks))]
|
67 |
+
return reranked_chunks[:10]
|
68 |
+
|
69 |
+
def process_query(query):
|
70 |
+
# Use global variables
|
71 |
+
global available_car_models, collection
|
72 |
+
|
73 |
+
car_model = is_car_model_available(query, available_car_models)
|
74 |
+
if not car_model:
|
75 |
+
return "The manual for the specified car model is not present."
|
76 |
+
|
77 |
+
# Initial retrieval from ChromaDB
|
78 |
+
results = collection.query(
|
79 |
+
query_texts=[query],
|
80 |
+
n_results=50,
|
81 |
+
where={"car_model": car_model},
|
82 |
+
include=['documents', 'metadatas']
|
83 |
+
)
|
84 |
+
|
85 |
+
if not results['documents']:
|
86 |
+
return "No relevant information found in the manual."
|
87 |
+
|
88 |
+
# Extract chunks and metadata
|
89 |
+
chunks = results['documents'][0]
|
90 |
+
metadatas = results['metadatas'][0]
|
91 |
+
|
92 |
+
reranked_chunks = colbert_rerank(query, chunks)
|
93 |
+
final_context = " ".join(reranked_chunks[:10])
|
94 |
+
|
95 |
+
answer = call_Llama_api(query, final_context)
|
96 |
+
|
97 |
+
# Prepare citations
|
98 |
+
citations = [
|
99 |
+
f"Page {meta.get('page_number', 'N/A')}" for meta in metadatas[:5]
|
100 |
+
]
|
101 |
+
|
102 |
+
citations_text = "Citations:\n" + "\n".join(citations)
|
103 |
+
|
104 |
+
return f"{answer}\n\n{citations_text}"
|
105 |
+
|
106 |
+
# Initialize global variables
|
107 |
+
def initialize():
|
108 |
+
global collection, available_car_models, ranker
|
109 |
+
|
110 |
+
# Check for CUDA availability
|
111 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
112 |
+
print(f"Using device: {device}")
|
113 |
+
|
114 |
+
# Initialize embedding model
|
115 |
+
embedding_function = embedding_functions.SentenceTransformerEmbeddingFunction(
|
116 |
+
model_name="all-MiniLM-L12-v2", device=device
|
117 |
+
)
|
118 |
+
|
119 |
+
# Load the persisted ChromaDB collection
|
120 |
+
client = PersistentClient(path="./chromadb")
|
121 |
+
|
122 |
+
# Get the collection
|
123 |
+
collection_name = "car_manuals5"
|
124 |
+
collection = client.get_collection(
|
125 |
+
name=collection_name,
|
126 |
+
embedding_function=embedding_function
|
127 |
+
)
|
128 |
+
|
129 |
+
# Set available car models
|
130 |
+
available_car_models = ['TIAGO', 'Astor']
|
131 |
+
|
132 |
+
# Initialize the ranker
|
133 |
+
ranker = Reranker("answerdotai/answerai-colbert-small-v1", model_type='colbert')
|
134 |
+
|
135 |
+
# Call initialize function
|
136 |
+
initialize()
|
137 |
+
|
138 |
+
# Set up the Gradio interface
|
139 |
+
iface = gr.Interface(
|
140 |
+
fn=process_query,
|
141 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder='Enter your question here...'),
|
142 |
+
outputs='text',
|
143 |
+
title='Car Manual Assistant',
|
144 |
+
description='Ask a question about your car manual.',
|
145 |
+
)
|
146 |
+
|
147 |
+
if __name__ == "__main__":
|
148 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
chromadb/chroma.sqlite3
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:481a2f22b50f9edd260645533393020b100f9c8e43ba5393925af96c02af9a2f
|
3 |
+
size 6451200
|
chromadb/e820442b-1d6c-4933-8a2c-981f60377458/data_level0.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cf95cb4ad00dbb2be6ce91b7143b22b48c7583817cc57b4fc153791554a14132
|
3 |
+
size 1676000
|
chromadb/e820442b-1d6c-4933-8a2c-981f60377458/header.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e87a1dc8bcae6f2c4bea6d5dd5005454d4dace8637dae29bff3c037ea771411e
|
3 |
+
size 100
|
chromadb/e820442b-1d6c-4933-8a2c-981f60377458/length.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:03e1219ac9d4a1a30d3d5f9f3dfc60df85e0844f2b73f04e8f641cc4a101a470
|
3 |
+
size 4000
|
chromadb/e820442b-1d6c-4933-8a2c-981f60377458/link_lists.bin
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# requirements.txt
|
2 |
+
|
3 |
+
gradio
|
4 |
+
torch
|
5 |
+
sentence_transformers
|
6 |
+
pdfplumber
|
7 |
+
chromadb
|
8 |
+
transformers
|
9 |
+
rerankers
|
10 |
+
groq
|