Atulkumar001 commited on
Commit
7eca6c7
·
verified ·
1 Parent(s): 346f7d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -96
app.py CHANGED
@@ -9,8 +9,12 @@ from sentence_transformers import (
9
  SentenceTransformer
10
  )
11
 
 
 
 
 
12
  # =====================================================
13
- # BUILD DATABASE IF NEEDED
14
  # =====================================================
15
 
16
  def build_database_if_needed():
@@ -27,37 +31,24 @@ def build_database_if_needed():
27
  index_exists and metadata_exists
28
  ):
29
 
30
- print(
31
- "Building database..."
32
- )
33
-
34
  subprocess.run(
35
- ["python", "build_database.py"]
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"
@@ -71,16 +62,28 @@ with open(
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]
@@ -95,99 +98,100 @@ def search_entertainment(query):
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(
@@ -195,7 +199,7 @@ with gr.Blocks(
195
  )
196
 
197
  button.click(
198
- fn=search_entertainment,
199
  inputs=query,
200
  outputs=output
201
  )
 
9
  SentenceTransformer
10
  )
11
 
12
+ from huggingface_hub import (
13
+ InferenceClient
14
+ )
15
+
16
  # =====================================================
17
+ # BUILD DB IF NEEDED
18
  # =====================================================
19
 
20
  def build_database_if_needed():
 
31
  index_exists and metadata_exists
32
  ):
33
 
 
 
 
 
34
  subprocess.run(
35
+ ["python",
36
+ "build_database.py"]
 
 
 
37
  )
38
 
39
+ build_database_if_needed()
 
 
 
 
 
 
40
 
41
  # =====================================================
42
+ # LOAD EMBEDDING MODEL
43
  # =====================================================
44
 
 
 
45
  model = SentenceTransformer(
46
  "all-MiniLM-L6-v2"
47
  )
48
 
49
+ # =====================================================
50
+ # LOAD VECTOR DB
51
+ # =====================================================
52
 
53
  index = faiss.read_index(
54
  "vector_store/faiss_index.bin"
 
62
  metadata = pickle.load(f)
63
 
64
  # =====================================================
65
+ # LOAD LLAMA 3
66
  # =====================================================
67
 
68
+ hf_token = os.getenv(
69
+ "HF_TOKEN"
70
+ )
71
 
72
+ client = InferenceClient(
73
+ model=
74
+ "meta-llama/Meta-Llama-3-8B-Instruct",
75
 
76
+ token=hf_token
77
+ )
78
+
79
+ # =====================================================
80
+ # AI SEARCH
81
+ # =====================================================
82
+
83
+ def entertainment_ai(query):
84
+
85
+ if not query.strip():
86
+ return "Please ask something."
87
 
88
  query_embedding = model.encode(
89
  [query]
 
98
  5
99
  )
100
 
101
+ retrieved_context = []
102
 
103
  for idx in indices[0]:
104
 
105
  doc = metadata[idx]
106
 
107
+ retrieved_context.append(
108
+ doc["context"]
 
 
 
 
 
 
 
 
 
 
 
 
109
  )
110
 
111
+ combined_context = (
112
+ "\n\n---\n\n".join(
113
+ retrieved_context
114
+ )
115
  )
116
 
117
+ messages = [
118
+ {
119
+ "role":
120
+ "system",
121
+
122
+ "content":
123
+ """
124
+ You are Universal
125
+ Entertainment AI.
126
+
127
+ Rules:
128
+ - Use ONLY retrieved context
129
+ - Never invent lore
130
+ - Be accurate
131
+ - Explain naturally
132
+ """
133
+ },
134
+
135
+ {
136
+ "role":
137
+ "user",
138
+
139
+ "content":
140
+ f"""
141
+ Question:
142
+ {query}
143
+
144
+ Context:
145
+ {combined_context}
146
+ """
147
+ }
148
+ ]
149
+
150
+ try:
151
+
152
+ response = (
153
+ client.chat.completions.create(
154
+ messages=messages,
155
+ max_tokens=250,
156
+ temperature=0.2
157
+ )
158
+ )
159
 
160
+ return (
161
+ response
162
+ .choices[0]
163
+ .message.content
164
+ )
165
 
166
+ except Exception as e:
 
167
 
168
+ return (
169
+ f"Error: {str(e)}"
170
+ )
 
171
 
172
+ # =====================================================
173
+ # UI
174
+ # =====================================================
175
 
176
+ with gr.Blocks() as demo:
 
 
 
 
177
 
178
  gr.Markdown(
179
+ "# 🎬 Universal Entertainment AI"
180
  )
181
 
182
+ gr.Markdown(
183
+ "Ask anything about TV shows."
 
184
  )
185
 
186
  query = gr.Textbox(
187
+ label="Question",
188
  placeholder=
189
+ "Why does Reddington want Elizabeth?"
190
  )
191
 
192
  output = gr.Textbox(
193
+ label="AI Answer",
194
+ lines=12
195
  )
196
 
197
  button = gr.Button(
 
199
  )
200
 
201
  button.click(
202
+ entertainment_ai,
203
  inputs=query,
204
  outputs=output
205
  )