Atulkumar001 commited on
Commit
f42400b
·
verified ·
1 Parent(s): 33345f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import os
3
- import subprocess
4
  import pickle
5
  import faiss
6
  import numpy as np
@@ -14,32 +13,36 @@ from huggingface_hub import (
14
  )
15
 
16
  # =====================================================
17
- # BUILD DB IF NEEDED
18
  # =====================================================
19
 
20
- try:
21
- build_database_if_needed()
22
- except Exception as e:
23
- print(e)
24
-
25
- index_exists = os.path.exists(
26
  "vector_store/faiss_index.bin"
27
  )
28
-
29
- metadata_exists = os.path.exists(
30
  "vector_store/metadata.pkl"
31
  )
 
32
 
33
- if not (
34
- index_exists and metadata_exists
35
- ):
36
 
37
- subprocess.run(
38
- ["python",
39
- "build_database.py"]
40
- )
41
 
42
- build_database_if_needed()
 
 
 
 
 
 
 
 
 
 
43
 
44
  # =====================================================
45
  # LOAD EMBEDDING MODEL
@@ -50,22 +53,7 @@ model = SentenceTransformer(
50
  )
51
 
52
  # =====================================================
53
- # LOAD VECTOR DB
54
- # =====================================================
55
-
56
- index = faiss.read_index(
57
- "vector_store/faiss_index.bin"
58
- )
59
-
60
- with open(
61
- "vector_store/metadata.pkl",
62
- "rb"
63
- ) as f:
64
-
65
- metadata = pickle.load(f)
66
-
67
- # =====================================================
68
- # LOAD LLAMA 3
69
  # =====================================================
70
 
71
  hf_token = os.getenv(
@@ -85,8 +73,19 @@ client = InferenceClient(
85
 
86
  def entertainment_ai(query):
87
 
 
 
 
 
 
 
 
 
88
  if not query.strip():
89
- return "Please ask something."
 
 
 
90
 
91
  query_embedding = model.encode(
92
  [query]
@@ -98,7 +97,7 @@ def entertainment_ai(query):
98
 
99
  distances, indices = index.search(
100
  query_embedding,
101
- 5
102
  )
103
 
104
  retrieved_context = []
@@ -118,6 +117,7 @@ def entertainment_ai(query):
118
  )
119
 
120
  messages = [
 
121
  {
122
  "role":
123
  "system",
@@ -128,10 +128,9 @@ def entertainment_ai(query):
128
  Entertainment AI.
129
 
130
  Rules:
131
- - Use ONLY retrieved context
132
  - Never invent lore
133
- - Be accurate
134
- - Explain naturally
135
  """
136
  },
137
 
@@ -155,7 +154,7 @@ def entertainment_ai(query):
155
  response = (
156
  client.chat.completions.create(
157
  messages=messages,
158
- max_tokens=250,
159
  temperature=0.2
160
  )
161
  )
@@ -183,18 +182,18 @@ with gr.Blocks() as demo:
183
  )
184
 
185
  gr.Markdown(
186
- "Ask anything about TV shows."
187
  )
188
 
189
  query = gr.Textbox(
190
  label="Question",
191
  placeholder=
192
- "Why does Reddington want Elizabeth?"
193
  )
194
 
195
  output = gr.Textbox(
196
  label="AI Answer",
197
- lines=12
198
  )
199
 
200
  button = gr.Button(
 
1
  import gradio as gr
2
  import os
 
3
  import pickle
4
  import faiss
5
  import numpy as np
 
13
  )
14
 
15
  # =====================================================
16
+ # LOAD DATABASE
17
  # =====================================================
18
 
19
+ DATABASE_READY = (
20
+ os.path.exists(
 
 
 
 
21
  "vector_store/faiss_index.bin"
22
  )
23
+ and
24
+ os.path.exists(
25
  "vector_store/metadata.pkl"
26
  )
27
+ )
28
 
29
+ if DATABASE_READY:
 
 
30
 
31
+ index = faiss.read_index(
32
+ "vector_store/faiss_index.bin"
33
+ )
 
34
 
35
+ with open(
36
+ "vector_store/metadata.pkl",
37
+ "rb"
38
+ ) as f:
39
+
40
+ metadata = pickle.load(f)
41
+
42
+ else:
43
+
44
+ index = None
45
+ metadata = None
46
 
47
  # =====================================================
48
  # LOAD EMBEDDING MODEL
 
53
  )
54
 
55
  # =====================================================
56
+ # LOAD LLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # =====================================================
58
 
59
  hf_token = os.getenv(
 
73
 
74
  def entertainment_ai(query):
75
 
76
+ if index is None:
77
+
78
+ return """
79
+ ⚡ Database still loading.
80
+
81
+ Refresh after 1–2 minutes.
82
+ """
83
+
84
  if not query.strip():
85
+
86
+ return (
87
+ "Please ask something."
88
+ )
89
 
90
  query_embedding = model.encode(
91
  [query]
 
97
 
98
  distances, indices = index.search(
99
  query_embedding,
100
+ 3
101
  )
102
 
103
  retrieved_context = []
 
117
  )
118
 
119
  messages = [
120
+
121
  {
122
  "role":
123
  "system",
 
128
  Entertainment AI.
129
 
130
  Rules:
131
+ - Answer ONLY from context
132
  - Never invent lore
133
+ - Be concise
 
134
  """
135
  },
136
 
 
154
  response = (
155
  client.chat.completions.create(
156
  messages=messages,
157
+ max_tokens=200,
158
  temperature=0.2
159
  )
160
  )
 
182
  )
183
 
184
  gr.Markdown(
185
+ "Ask questions about TV shows."
186
  )
187
 
188
  query = gr.Textbox(
189
  label="Question",
190
  placeholder=
191
+ "Who is Ranko Zamani?"
192
  )
193
 
194
  output = gr.Textbox(
195
  label="AI Answer",
196
+ lines=10
197
  )
198
 
199
  button = gr.Button(