Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,12 @@ from sentence_transformers import (
|
|
| 9 |
SentenceTransformer
|
| 10 |
)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# =====================================================
|
| 13 |
-
# BUILD
|
| 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",
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return (
|
| 39 |
-
"⚡ Building database..."
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"✅ Database Ready"
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
status_message = (
|
| 47 |
-
build_database_if_needed()
|
| 48 |
-
)
|
| 49 |
|
| 50 |
# =====================================================
|
| 51 |
-
# LOAD
|
| 52 |
# =====================================================
|
| 53 |
|
| 54 |
-
print("Loading embedding model...")
|
| 55 |
-
|
| 56 |
model = SentenceTransformer(
|
| 57 |
"all-MiniLM-L6-v2"
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 75 |
# =====================================================
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
query_embedding = model.encode(
|
| 86 |
[query]
|
|
@@ -95,99 +98,100 @@ def search_entertainment(query):
|
|
| 95 |
5
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
| 99 |
|
| 100 |
for idx in indices[0]:
|
| 101 |
|
| 102 |
doc = metadata[idx]
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 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 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
bold;
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
"""
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
|
| 167 |
-
|
| 168 |
-
<div class="title">
|
| 169 |
-
🎬 Universal Entertainment AI
|
| 170 |
-
</div>
|
| 171 |
-
""")
|
| 172 |
|
| 173 |
gr.Markdown(
|
| 174 |
-
"
|
| 175 |
)
|
| 176 |
|
| 177 |
-
gr.
|
| 178 |
-
|
| 179 |
-
label="System Status"
|
| 180 |
)
|
| 181 |
|
| 182 |
query = gr.Textbox(
|
| 183 |
-
label="
|
| 184 |
placeholder=
|
| 185 |
-
"
|
| 186 |
)
|
| 187 |
|
| 188 |
output = gr.Textbox(
|
| 189 |
-
label="AI
|
| 190 |
-
lines=
|
| 191 |
)
|
| 192 |
|
| 193 |
button = gr.Button(
|
|
@@ -195,7 +199,7 @@ with gr.Blocks(
|
|
| 195 |
)
|
| 196 |
|
| 197 |
button.click(
|
| 198 |
-
|
| 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 |
)
|