Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,78 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="
|
| 50 |
-
gr.
|
| 51 |
-
gr.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
|
|
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from rpc import load_index, generate
|
| 3 |
|
| 4 |
+
def setup():
|
| 5 |
+
""" Downloads the memory mapped vector index (~10GB), installs NGT and loads the index"""
|
| 6 |
+
import os
|
| 7 |
+
import requests
|
| 8 |
+
import subprocess
|
| 9 |
+
from stream_unzip import stream_unzip
|
| 10 |
+
|
| 11 |
+
DATASET_URL = os.environ.get("DATASET_URL")
|
| 12 |
+
if not DATASET_URL:
|
| 13 |
+
raise ValueError("DATASET_URL must be set in the environment")
|
| 14 |
+
extract_dir = "/dev/shm/rpc-vecdb"
|
| 15 |
+
os.makedirs(extract_dir, exist_ok=True)
|
| 16 |
+
response = requests.get(DATASET_URL, stream=True)
|
| 17 |
+
response.raise_for_status()
|
| 18 |
+
|
| 19 |
+
print("Starting streaming extraction to /dev/shm...")
|
| 20 |
+
for filename, filesize, file_iter in stream_unzip(response.iter_content(chunk_size=8192)):
|
| 21 |
+
if isinstance(filename, bytes):
|
| 22 |
+
filename = filename.decode('utf-8')
|
| 23 |
+
file_path = os.path.join(extract_dir, filename)
|
| 24 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 25 |
+
with open(file_path, 'wb') as f_out:
|
| 26 |
+
for chunk in file_iter:
|
| 27 |
+
f_out.write(chunk)
|
| 28 |
+
print(f"Extracted: {filename} -> {file_path}")
|
| 29 |
+
|
| 30 |
+
files = os.listdir(extract_dir)
|
| 31 |
+
files = [f for f in files if os.path.isfile(os.path.join(extract_dir, f))]
|
| 32 |
+
for f in files: print(f)
|
| 33 |
+
print("Index extracted")
|
| 34 |
+
|
| 35 |
+
print("Installing NGT...")
|
| 36 |
+
subprocess.check_call(["bash", "install_ngt.sh"])
|
| 37 |
+
print("NGT installed")
|
| 38 |
+
|
| 39 |
+
print("Loading index...")
|
| 40 |
+
load_index(extract_dir)
|
| 41 |
+
print("Index loaded")
|
| 42 |
+
|
| 43 |
|
| 44 |
|
| 45 |
def respond(
|
| 46 |
message,
|
| 47 |
history: list[tuple[str, str]],
|
| 48 |
+
user_name,
|
| 49 |
+
ai_name,
|
| 50 |
+
use_rpc,
|
| 51 |
max_tokens,
|
| 52 |
temperature,
|
|
|
|
| 53 |
):
|
| 54 |
+
prompt = "<s>"
|
| 55 |
+
|
| 56 |
+
for m in history:
|
| 57 |
+
prompt += f"{user_name}: {m[0]}\n{ai_name}: {m[1]}\n"
|
| 58 |
+
|
| 59 |
+
for response in generate(prompt, use_rpc=use_rpc, max_tokens=max_tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
yield response
|
| 61 |
|
| 62 |
|
| 63 |
+
|
|
|
|
|
|
|
| 64 |
demo = gr.ChatInterface(
|
| 65 |
respond,
|
| 66 |
additional_inputs=[
|
| 67 |
+
gr.Textbox(value="Peter", label="User name"),
|
| 68 |
+
gr.Textbox(value="Sarah", label="Assistant name"),
|
| 69 |
+
gr.Checkbox(
|
| 70 |
+
label="Use RPC",
|
| 71 |
+
info="Compare Normal vs. RPC-Enhanced Model",
|
| 72 |
+
value=True
|
|
|
|
|
|
|
|
|
|
| 73 |
),
|
| 74 |
+
gr.Slider(minimum=1, maximum=320, value=128, step=1, label="Max new tokens"),
|
| 75 |
+
gr.Slider(minimum=0.1, maximum=3.0, value=0.2, step=0.1, label="Temperature (only used without RPC)"),
|
| 76 |
],
|
| 77 |
)
|
| 78 |
|