Hari-67 commited on
Commit
b5d46b8
·
verified ·
1 Parent(s): 28fb32c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -6
app.py CHANGED
@@ -1,14 +1,97 @@
 
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return f"Hello {name}, your AI Twin is under construction!"
 
5
 
6
- iface = gr.Interface(
7
- fn=greet,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  inputs="text",
9
  outputs="text",
10
  title="Hari's AI Twin",
11
- description="Ask me anything about my profile!"
12
  )
13
 
14
- iface.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import faiss
4
+ import torch
5
+ from sentence_transformers import SentenceTransformer
6
+ from transformers import pipeline
7
 
8
+ # -----------------------------
9
+ # Load Documents (Real + Synthetic)
10
+ # -----------------------------
11
 
12
+ def load_documents():
13
+ docs = []
14
+
15
+ # Load real KB
16
+ for file in os.listdir("knowledge_base"):
17
+ with open(f"knowledge_base/{file}", "r", encoding="utf-8") as f:
18
+ docs.append(f.read())
19
+
20
+ # Load synthetic data
21
+ for file in os.listdir("synthetic_data"):
22
+ with open(f"synthetic_data/{file}", "r", encoding="utf-8") as f:
23
+ docs.append(f.read())
24
+
25
+ return docs
26
+
27
+ documents = load_documents()
28
+
29
+ # -----------------------------
30
+ # Create Embeddings
31
+ # -----------------------------
32
+
33
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
34
+ embeddings = embed_model.encode(documents)
35
+
36
+ dimension = embeddings.shape[1]
37
+ index = faiss.IndexFlatL2(dimension)
38
+ index.add(embeddings)
39
+
40
+ # -----------------------------
41
+ # Load Generation Model (HF)
42
+ # -----------------------------
43
+
44
+ generator = pipeline(
45
+ "text-generation",
46
+ model="distilgpt2",
47
+ device=0 if torch.cuda.is_available() else -1
48
+ )
49
+
50
+ # -----------------------------
51
+ # Retrieval Function
52
+ # -----------------------------
53
+
54
+ def retrieve(query, k=3):
55
+ query_embedding = embed_model.encode([query])
56
+ distances, indices = index.search(query_embedding, k)
57
+ return [documents[i] for i in indices[0]]
58
+
59
+ # -----------------------------
60
+ # RAG Function
61
+ # -----------------------------
62
+
63
+ def generate_answer(query):
64
+ context = retrieve(query)
65
+
66
+ prompt = f"""
67
+ You are Hari's AI Twin.
68
+ Answer ONLY using the context below.
69
+ If answer is not found, say: Information not found in profile.
70
+
71
+ Context:
72
+ {context}
73
+
74
+ Question: {query}
75
+
76
+ Answer:
77
+ """
78
+
79
+ output = generator(prompt, max_length=400, num_return_sequences=1)
80
+ return output[0]["generated_text"].split("Answer:")[-1].strip()
81
+
82
+ # -----------------------------
83
+ # Gradio UI
84
+ # -----------------------------
85
+
86
+ def chatbot(query):
87
+ return generate_answer(query)
88
+
89
+ interface = gr.Interface(
90
+ fn=chatbot,
91
  inputs="text",
92
  outputs="text",
93
  title="Hari's AI Twin",
94
+ description="Ask me anything about my professional journey."
95
  )
96
 
97
+ interface.launch()