Update app.py
Browse files
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 |
-
#
|
| 18 |
# =====================================================
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
except Exception as e:
|
| 23 |
-
print(e)
|
| 24 |
-
|
| 25 |
-
index_exists = os.path.exists(
|
| 26 |
"vector_store/faiss_index.bin"
|
| 27 |
)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
"vector_store/metadata.pkl"
|
| 31 |
)
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
index_exists and metadata_exists
|
| 35 |
-
):
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# =====================================================
|
| 45 |
# LOAD EMBEDDING MODEL
|
|
@@ -50,22 +53,7 @@ model = SentenceTransformer(
|
|
| 50 |
)
|
| 51 |
|
| 52 |
# =====================================================
|
| 53 |
-
# LOAD
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
-
|
| 132 |
- Never invent lore
|
| 133 |
-
- Be
|
| 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=
|
| 159 |
temperature=0.2
|
| 160 |
)
|
| 161 |
)
|
|
@@ -183,18 +182,18 @@ with gr.Blocks() as demo:
|
|
| 183 |
)
|
| 184 |
|
| 185 |
gr.Markdown(
|
| 186 |
-
"Ask
|
| 187 |
)
|
| 188 |
|
| 189 |
query = gr.Textbox(
|
| 190 |
label="Question",
|
| 191 |
placeholder=
|
| 192 |
-
"
|
| 193 |
)
|
| 194 |
|
| 195 |
output = gr.Textbox(
|
| 196 |
label="AI Answer",
|
| 197 |
-
lines=
|
| 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(
|