Atulkumar001 commited on
Commit
5fa1df7
·
verified ·
1 Parent(s): c04d9fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +333 -73
app.py CHANGED
@@ -1,29 +1,22 @@
 
 
 
1
  import gradio as gr
2
  import os
3
  import pickle
4
  import faiss
5
  import numpy as np
6
-
7
- from sentence_transformers import (
8
- SentenceTransformer
9
- )
10
-
11
- from huggingface_hub import (
12
- InferenceClient
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:
@@ -48,6 +41,8 @@ else:
48
  # LOAD EMBEDDING MODEL
49
  # =====================================================
50
 
 
 
51
  model = SentenceTransformer(
52
  "all-MiniLM-L6-v2"
53
  )
@@ -56,14 +51,10 @@ model = SentenceTransformer(
56
  # LOAD LLM
57
  # =====================================================
58
 
59
- hf_token = os.getenv(
60
- "HF_TOKEN"
61
- )
62
 
63
  client = InferenceClient(
64
- model=
65
- "meta-llama/Meta-Llama-3-8B-Instruct",
66
-
67
  token=hf_token
68
  )
69
 
@@ -71,24 +62,30 @@ client = InferenceClient(
71
  # AI SEARCH
72
  # =====================================================
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]
92
  )
93
 
94
  query_embedding = np.array(
@@ -97,10 +94,11 @@ Refresh after 1–2 minutes.
97
 
98
  distances, indices = index.search(
99
  query_embedding,
100
- 3
101
  )
102
 
103
  retrieved_context = []
 
104
 
105
  for idx in indices[0]:
106
 
@@ -110,6 +108,14 @@ Refresh after 1–2 minutes.
110
  doc["context"]
111
  )
112
 
 
 
 
 
 
 
 
 
113
  combined_context = (
114
  "\n\n---\n\n".join(
115
  retrieved_context
@@ -119,33 +125,31 @@ Refresh after 1–2 minutes.
119
  messages = [
120
 
121
  {
122
- "role":
123
- "system",
124
-
125
- "content":
126
- """
127
- You are Universal
128
- Entertainment AI.
129
-
130
- Rules:
131
- - Answer ONLY from context
132
- - Never invent lore
133
- - Be concise
134
- """
135
  },
136
 
137
  {
138
- "role":
139
- "user",
140
-
141
  "content":
142
  f"""
143
- Question:
144
- {query}
145
 
146
- Context:
147
- {combined_context}
148
- """
149
  }
150
  ]
151
 
@@ -154,12 +158,12 @@ Refresh after 1–2 minutes.
154
  response = (
155
  client.chat.completions.create(
156
  messages=messages,
157
- max_tokens=200,
158
- temperature=0.2
159
  )
160
  )
161
 
162
- return (
163
  response
164
  .choices[0]
165
  .message.content
@@ -167,43 +171,299 @@ Refresh after 1–2 minutes.
167
 
168
  except Exception as e:
169
 
170
- return (
171
- f"Error: {str(e)}"
172
  )
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  # =====================================================
175
  # UI
176
  # =====================================================
177
 
178
- with gr.Blocks() as demo:
 
 
 
179
 
180
- gr.Markdown(
181
- "# 🎬 Universal Entertainment AI"
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(
200
- "🚀 Analyze"
 
 
 
 
 
 
 
 
201
  )
202
 
203
- button.click(
204
  entertainment_ai,
205
- inputs=query,
206
- outputs=output
 
 
 
 
 
 
207
  )
208
 
209
  demo.launch()
 
1
+ # PREMIUM NETFLIX × OPENAI APP.PY
2
+ # Copy-Paste Entire File
3
+
4
  import gradio as gr
5
  import os
6
  import pickle
7
  import faiss
8
  import numpy as np
9
+ from sentence_transformers import SentenceTransformer
10
+ from huggingface_hub import InferenceClient
 
 
 
 
 
 
11
 
12
  # =====================================================
13
  # LOAD DATABASE
14
  # =====================================================
15
 
16
  DATABASE_READY = (
17
+ os.path.exists("vector_store/faiss_index.bin")
 
 
18
  and
19
+ os.path.exists("vector_store/metadata.pkl")
 
 
20
  )
21
 
22
  if DATABASE_READY:
 
41
  # LOAD EMBEDDING MODEL
42
  # =====================================================
43
 
44
+ print("Loading embedding model...")
45
+
46
  model = SentenceTransformer(
47
  "all-MiniLM-L6-v2"
48
  )
 
51
  # LOAD LLM
52
  # =====================================================
53
 
54
+ hf_token = os.getenv("HF_TOKEN")
 
 
55
 
56
  client = InferenceClient(
57
+ model="meta-llama/Meta-Llama-3-8B-Instruct",
 
 
58
  token=hf_token
59
  )
60
 
 
62
  # AI SEARCH
63
  # =====================================================
64
 
65
+ def entertainment_ai(message, history):
66
 
67
  if index is None:
68
 
69
+ history.append(
70
+ {
71
+ "role": "assistant",
72
+ "content":
73
+ """
74
  ⚡ Database still loading.
75
 
76
+ Please wait 1–2 minutes
77
+ and refresh.
78
  """
79
+ }
80
+ )
81
 
82
+ return history, ""
83
 
84
+ if not message.strip():
85
+ return history, ""
 
86
 
87
  query_embedding = model.encode(
88
+ [message]
89
  )
90
 
91
  query_embedding = np.array(
 
94
 
95
  distances, indices = index.search(
96
  query_embedding,
97
+ 5
98
  )
99
 
100
  retrieved_context = []
101
+ retrieved_metadata = []
102
 
103
  for idx in indices[0]:
104
 
 
108
  doc["context"]
109
  )
110
 
111
+ retrieved_metadata.append(
112
+ f"""
113
+ 🎬 {doc['series']}
114
+ S{doc['season']}E{doc['episode']}
115
+ • {doc['title']}
116
+ """
117
+ )
118
+
119
  combined_context = (
120
  "\n\n---\n\n".join(
121
  retrieved_context
 
125
  messages = [
126
 
127
  {
128
+ "role": "system",
129
+ "content": """
130
+ You are CineMind AI,
131
+ an advanced entertainment
132
+ analysis assistant.
133
+
134
+ Rules:
135
+ - Answer ONLY using context
136
+ - Never invent plot details
137
+ - Explain naturally
138
+ - Sound intelligent
139
+ - Keep answers concise
140
+ """
141
  },
142
 
143
  {
144
+ "role": "user",
 
 
145
  "content":
146
  f"""
147
+ Question:
148
+ {message}
149
 
150
+ Retrieved Context:
151
+ {combined_context}
152
+ """
153
  }
154
  ]
155
 
 
158
  response = (
159
  client.chat.completions.create(
160
  messages=messages,
161
+ max_tokens=220,
162
+ temperature=0.25
163
  )
164
  )
165
 
166
+ ai_answer = (
167
  response
168
  .choices[0]
169
  .message.content
 
171
 
172
  except Exception as e:
173
 
174
+ ai_answer = (
175
+ f"❌ Error: {str(e)}"
176
  )
177
 
178
+ metadata_text = "\n".join(
179
+ retrieved_metadata
180
+ )
181
+
182
+ final_response = f"""
183
+ {ai_answer}
184
+
185
+ ━━━━━━━━━━━━━━━━━━━
186
+
187
+ **Retrieved Sources**
188
+
189
+ {metadata_text}
190
+ """
191
+
192
+ history.append(
193
+ {
194
+ "role": "user",
195
+ "content": message
196
+ }
197
+ )
198
+
199
+ history.append(
200
+ {
201
+ "role": "assistant",
202
+ "content": final_response
203
+ }
204
+ )
205
+
206
+ return history, ""
207
+
208
+ # =====================================================
209
+ # PREMIUM CSS
210
+ # =====================================================
211
+
212
+ custom_css = """
213
+
214
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Orbitron:wght@600&display=swap');
215
+
216
+ body {
217
+ background: #0d0d0d !important;
218
+ }
219
+
220
+ .gradio-container {
221
+
222
+ background:
223
+ radial-gradient(
224
+ circle at top,
225
+ #1b0f10 0%,
226
+ #0d0d0d 45%,
227
+ #050505 100%
228
+ ) !important;
229
+
230
+ color: white !important;
231
+ font-family: 'Inter', sans-serif;
232
+ }
233
+
234
+ /* HERO */
235
+
236
+ .hero-title {
237
+
238
+ text-align: center;
239
+
240
+ font-size: 56px;
241
+
242
+ font-weight: 700;
243
+
244
+ letter-spacing: -2px;
245
+
246
+ background:
247
+ linear-gradient(
248
+ 90deg,
249
+ #ff3b3b,
250
+ #ff6a6a
251
+ );
252
+
253
+ -webkit-background-clip: text;
254
+ -webkit-text-fill-color: transparent;
255
+
256
+ margin-bottom: 6px;
257
+ }
258
+
259
+ .hero-subtitle {
260
+
261
+ text-align: center;
262
+
263
+ color: #a0a0a0;
264
+
265
+ font-size: 18px;
266
+
267
+ margin-bottom: 25px;
268
+ }
269
+
270
+ /* CHAT */
271
+
272
+ .chat-window {
273
+
274
+ border-radius: 30px !important;
275
+
276
+ border:
277
+ 1px solid rgba(
278
+ 255,
279
+ 255,
280
+ 255,
281
+ 0.08
282
+ ) !important;
283
+
284
+ background:
285
+ rgba(
286
+ 255,
287
+ 255,
288
+ 255,
289
+ 0.04
290
+ ) !important;
291
+
292
+ backdrop-filter:
293
+ blur(20px);
294
+
295
+ box-shadow:
296
+ 0 0 50px rgba(
297
+ 255,
298
+ 0,
299
+ 0,
300
+ 0.08
301
+ );
302
+ }
303
+
304
+ /* INPUT */
305
+
306
+ textarea {
307
+
308
+ background:
309
+ rgba(
310
+ 255,
311
+ 255,
312
+ 255,
313
+ 0.06
314
+ ) !important;
315
+
316
+ color:
317
+ white !important;
318
+
319
+ border:
320
+ 1px solid rgba(
321
+ 255,
322
+ 255,
323
+ 255,
324
+ 0.08
325
+ ) !important;
326
+
327
+ border-radius:
328
+ 18px !important;
329
+
330
+ padding:
331
+ 18px !important;
332
+
333
+ font-size:
334
+ 16px !important;
335
+ }
336
+
337
+ /* BUTTON */
338
+
339
+ button {
340
+
341
+ background:
342
+ linear-gradient(
343
+ 135deg,
344
+ #ff2d2d,
345
+ #ff5e5e
346
+ ) !important;
347
+
348
+ color:
349
+ white !important;
350
+
351
+ border:
352
+ none !important;
353
+
354
+ border-radius:
355
+ 18px !important;
356
+
357
+ font-weight:
358
+ 600 !important;
359
+
360
+ transition:
361
+ 0.25s ease !important;
362
+
363
+ box-shadow:
364
+ 0 0 20px rgba(
365
+ 255,
366
+ 0,
367
+ 0,
368
+ 0.25
369
+ ) !important;
370
+ }
371
+
372
+ button:hover {
373
+
374
+ transform:
375
+ translateY(-2px);
376
+
377
+ box-shadow:
378
+ 0 0 30px rgba(
379
+ 255,
380
+ 0,
381
+ 0,
382
+ 0.35
383
+ ) !important;
384
+ }
385
+
386
+ footer {
387
+ display: none !important;
388
+ }
389
+
390
+ """
391
+
392
  # =====================================================
393
  # UI
394
  # =====================================================
395
 
396
+ with gr.Blocks(
397
+ css=custom_css,
398
+ theme=gr.themes.Soft()
399
+ ) as demo:
400
 
401
+ gr.HTML(
402
+ """
403
+ <div class="hero-title">
404
+ CineMind AI
405
+ </div>
406
 
407
+ <div class="hero-subtitle">
408
+ Netflix × OpenAI Entertainment Intelligence Engine
409
+ </div>
410
+ """
411
  )
412
 
413
+ chatbot = gr.Chatbot(
414
+ type="messages",
415
+ height=600,
416
+ elem_classes="chat-window"
417
  )
418
 
419
+ with gr.Row():
420
+
421
+ user_input = gr.Textbox(
422
+ placeholder=
423
+ "Ask anything about TV shows, characters, plots...",
424
+ lines=2,
425
+ scale=8
426
+ )
427
+
428
+ send_button = gr.Button(
429
+ "🎬 Analyze",
430
+ scale=1
431
+ )
432
+
433
+ gr.Examples(
434
+ examples=[
435
+ ["Who is Ranko Zamani?"],
436
+ ["Why does Reddington want Elizabeth?"],
437
+ ["Who is Walter White?"],
438
+ ["What is the Red Wedding?"],
439
+ ["Who is Homelander?"],
440
+ ["What happened to Rachel?"]
441
+ ],
442
+ inputs=user_input
443
  )
444
 
445
+ send_button.click(
446
+ entertainment_ai,
447
+ inputs=[
448
+ user_input,
449
+ chatbot
450
+ ],
451
+ outputs=[
452
+ chatbot,
453
+ user_input
454
+ ]
455
  )
456
 
457
+ user_input.submit(
458
  entertainment_ai,
459
+ inputs=[
460
+ user_input,
461
+ chatbot
462
+ ],
463
+ outputs=[
464
+ chatbot,
465
+ user_input
466
+ ]
467
  )
468
 
469
  demo.launch()