Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import pinecone
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
|
7 |
+
# β
Initialize Pinecone
|
8 |
+
pc = pinecone.Pinecone(api_key="pcsk_6awTRp_rSsr7eom3bSZXZZcnDLDwc87RnpU2Sp9WEzyEFdEj2TtiyRwjEfnaXswVjGqLi")
|
9 |
+
|
10 |
+
# β
Define Indexes
|
11 |
+
INDEXES = {
|
12 |
+
"YouTube": "youtube-data-index",
|
13 |
+
"Chrome": "chrome-history-index"
|
14 |
+
}
|
15 |
+
|
16 |
+
# β
Model paths (Hugging Face)
|
17 |
+
MODEL_PATHS = {
|
18 |
+
"YouTube": "Vishal3041/falcon_finetuned_llm",
|
19 |
+
"Chrome": "Vishal3041/TransNormerLLM_finetuned"
|
20 |
+
}
|
21 |
+
|
22 |
+
# β
Load Sentence Transformer for correct embedding size (384)
|
23 |
+
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
24 |
+
|
25 |
+
# β
Function to load model dynamically
|
26 |
+
def load_model(model_name):
|
27 |
+
model = AutoModelForCausalLM.from_pretrained(
|
28 |
+
model_name, trust_remote_code=True, device_map="auto"
|
29 |
+
)
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
31 |
+
return model, tokenizer
|
32 |
+
|
33 |
+
# β
Function to query Pinecone
|
34 |
+
def query_pinecone(query, app_selected):
|
35 |
+
""" Retrieves the most relevant results from Pinecone. """
|
36 |
+
index_name = INDEXES[app_selected]
|
37 |
+
index = pc.Index(index_name)
|
38 |
+
|
39 |
+
# Generate embedding for the query
|
40 |
+
query_vector = embedding_model.encode(query).tolist()
|
41 |
+
|
42 |
+
# Query Pinecone for relevant results
|
43 |
+
results = index.query(
|
44 |
+
vector=query_vector,
|
45 |
+
top_k=5,
|
46 |
+
include_metadata=True
|
47 |
+
)
|
48 |
+
|
49 |
+
# Format results for context
|
50 |
+
context_list = []
|
51 |
+
for res in results.get("matches", []):
|
52 |
+
metadata = res.get("metadata", {})
|
53 |
+
title = metadata.get("Title", "No Title")
|
54 |
+
timestamp = metadata.get("Timestamp", "No Date")
|
55 |
+
|
56 |
+
if app_selected == "Chrome":
|
57 |
+
formatted_entry = f"π **{title}**\n π *Visited on: {timestamp}*"
|
58 |
+
else:
|
59 |
+
watched_at = metadata.get("Watched At", "Unknown Date")
|
60 |
+
video_link = metadata.get("Video Link", "#")
|
61 |
+
formatted_entry = f"π¬ **[{title}]({video_link})**\n π
*Watched on: {watched_at}*"
|
62 |
+
|
63 |
+
context_list.append(formatted_entry)
|
64 |
+
|
65 |
+
return "\n\n".join(context_list) if context_list else "No relevant results found."
|
66 |
+
|
67 |
+
# β
Function to generate response
|
68 |
+
def generate_response(query, app_selected):
|
69 |
+
""" Handles RAG pipeline: fetches context + generates LLM response. """
|
70 |
+
|
71 |
+
# Load correct model from Hugging Face
|
72 |
+
model_name = MODEL_PATHS[app_selected]
|
73 |
+
model, tokenizer = load_model(model_name)
|
74 |
+
|
75 |
+
# Get relevant context
|
76 |
+
context = query_pinecone(query, app_selected)
|
77 |
+
|
78 |
+
# Format input prompt
|
79 |
+
input_text = f"Context: {context}\nUser Question: {query}\nAnswer:"
|
80 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
81 |
+
|
82 |
+
# Generate response
|
83 |
+
output = model.generate(input_ids, max_length=512, do_sample=True, top_p=0.9, temperature=0.7)
|
84 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
85 |
+
|
86 |
+
return response
|
87 |
+
|
88 |
+
# β
Gradio UI
|
89 |
+
def gradio_ui(query, app_selected):
|
90 |
+
return generate_response(query, app_selected)
|
91 |
+
|
92 |
+
# β
Create Gradio Interface
|
93 |
+
iface = gr.Interface(
|
94 |
+
fn=gradio_ui,
|
95 |
+
inputs=[
|
96 |
+
gr.Textbox(lines=2, placeholder="Type your question..."),
|
97 |
+
gr.Radio(["YouTube", "Chrome"], label="Select Application", value="YouTube")
|
98 |
+
],
|
99 |
+
outputs="text",
|
100 |
+
title="π Personal AI Assistant",
|
101 |
+
description="Chat with your YouTube or Chrome history using AI!"
|
102 |
+
)
|
103 |
+
|
104 |
+
# β
Launch Gradio UI
|
105 |
+
if __name__ == "__main__":
|
106 |
+
iface.launch()
|