Atulkumar001 commited on
Commit
346f7d2
Β·
verified Β·
1 Parent(s): 3e38cbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -8
app.py CHANGED
@@ -1,7 +1,17 @@
1
  import gradio as gr
2
  import os
3
  import subprocess
 
 
 
4
 
 
 
 
 
 
 
 
5
 
6
  def build_database_if_needed():
7
 
@@ -26,31 +36,168 @@ def build_database_if_needed():
26
  )
27
 
28
  return (
29
- "⚑ Building Entertainment Database..."
30
  )
31
 
32
  return (
33
- "βœ… Entertainment Database Ready"
34
  )
35
 
36
-
37
  status_message = (
38
  build_database_if_needed()
39
  )
40
 
41
- with gr.Blocks() as demo:
 
 
42
 
43
- gr.Markdown(
44
- "# 🎬 Universal Entertainment AI"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  )
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  gr.Markdown(
48
- "Recruiter-level TV & Movie Intelligence Engine"
49
  )
50
 
51
- status = gr.Textbox(
52
  value=status_message,
53
  label="System Status"
54
  )
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import subprocess
4
+ import pickle
5
+ import faiss
6
+ import numpy as np
7
 
8
+ from sentence_transformers import (
9
+ SentenceTransformer
10
+ )
11
+
12
+ # =====================================================
13
+ # BUILD DATABASE IF NEEDED
14
+ # =====================================================
15
 
16
  def build_database_if_needed():
17
 
 
36
  )
37
 
38
  return (
39
+ "⚑ Building database..."
40
  )
41
 
42
  return (
43
+ "βœ… Database Ready"
44
  )
45
 
 
46
  status_message = (
47
  build_database_if_needed()
48
  )
49
 
50
+ # =====================================================
51
+ # LOAD MODELS
52
+ # =====================================================
53
 
54
+ print("Loading embedding model...")
55
+
56
+ model = SentenceTransformer(
57
+ "all-MiniLM-L6-v2"
58
+ )
59
+
60
+ print("Loading vector DB...")
61
+
62
+ index = faiss.read_index(
63
+ "vector_store/faiss_index.bin"
64
+ )
65
+
66
+ with open(
67
+ "vector_store/metadata.pkl",
68
+ "rb"
69
+ ) as f:
70
+
71
+ metadata = pickle.load(f)
72
+
73
+ # =====================================================
74
+ # SEARCH ENGINE
75
+ # =====================================================
76
+
77
+ def search_entertainment(query):
78
+
79
+ if not query.strip():
80
+
81
+ return (
82
+ "Please ask a question."
83
+ )
84
+
85
+ query_embedding = model.encode(
86
+ [query]
87
+ )
88
+
89
+ query_embedding = np.array(
90
+ query_embedding
91
+ ).astype("float32")
92
+
93
+ distances, indices = index.search(
94
+ query_embedding,
95
+ 5
96
+ )
97
+
98
+ retrieved_docs = []
99
+
100
+ for idx in indices[0]:
101
+
102
+ doc = metadata[idx]
103
+
104
+ retrieved_docs.append(
105
+ f"""
106
+ 🎬 Series:
107
+ {doc['series']}
108
+
109
+ πŸ“Ί Season:
110
+ {doc['season']}
111
+
112
+ 🎞 Episode:
113
+ {doc['episode']}
114
+
115
+ πŸ“ Context:
116
+ {doc['context']}
117
+ """
118
+ )
119
+
120
+ return "\n\n".join(
121
+ retrieved_docs
122
  )
123
 
124
+ # =====================================================
125
+ # FUTURISTIC UI
126
+ # =====================================================
127
+
128
+ custom_css = """
129
+ body {
130
+ background:
131
+ #050816 !important;
132
+ }
133
+
134
+ .gradio-container {
135
+
136
+ background:
137
+ radial-gradient(
138
+ circle at top left,
139
+ #101935 0%,
140
+ #050816 60%,
141
+ #02030a 100%)
142
+ !important;
143
+
144
+ color:
145
+ white !important;
146
+ }
147
+
148
+ .title {
149
+ text-align:
150
+ center;
151
+
152
+ font-size:
153
+ 42px;
154
+
155
+ font-weight:
156
+ bold;
157
+
158
+ color:
159
+ cyan;
160
+ }
161
+ """
162
+
163
+ with gr.Blocks(
164
+ css=custom_css
165
+ ) as demo:
166
+
167
+ gr.HTML("""
168
+ <div class="title">
169
+ 🎬 Universal Entertainment AI
170
+ </div>
171
+ """)
172
+
173
  gr.Markdown(
174
+ "Ask anything about TV shows."
175
  )
176
 
177
+ gr.Textbox(
178
  value=status_message,
179
  label="System Status"
180
  )
181
 
182
+ query = gr.Textbox(
183
+ label="Ask Question",
184
+ placeholder=
185
+ "Example: Why did Homelander betray Starlight?"
186
+ )
187
+
188
+ output = gr.Textbox(
189
+ label="AI Response",
190
+ lines=20
191
+ )
192
+
193
+ button = gr.Button(
194
+ "πŸš€ Analyze"
195
+ )
196
+
197
+ button.click(
198
+ fn=search_entertainment,
199
+ inputs=query,
200
+ outputs=output
201
+ )
202
+
203
  demo.launch()