Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,14 @@ from llama_cpp import Llama
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from pydantic import BaseModel
|
| 8 |
-
from threading import Thread
|
| 9 |
import uvicorn
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# ----------------------------
|
| 12 |
# Model
|
| 13 |
# ----------------------------
|
|
@@ -31,8 +36,9 @@ llm("warmup", max_tokens=1)
|
|
| 31 |
# System Prompt
|
| 32 |
# ----------------------------
|
| 33 |
SYSTEM_PROMPT = """
|
| 34 |
-
You are
|
| 35 |
-
|
|
|
|
| 36 |
"""
|
| 37 |
|
| 38 |
# ----------------------------
|
|
@@ -44,24 +50,55 @@ def generate_response(message, history):
|
|
| 44 |
|
| 45 |
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 46 |
for h in history:
|
| 47 |
-
if isinstance(h, dict)
|
| 48 |
role = h["role"]
|
| 49 |
msg = h["message"]
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
prompt += f"<|im_start|>assistant\n{msg}<|im_end|>\n"
|
| 54 |
-
elif isinstance(h, (list, tuple)) and len(h) >= 2:
|
| 55 |
-
u, a = h[0], h[1]
|
| 56 |
prompt += f"<|im_start|>user\n{u}<|im_end|>\n<|im_start|>assistant\n{a}<|im_end|>\n"
|
| 57 |
|
| 58 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 59 |
|
| 60 |
output = ""
|
| 61 |
for token in llm(prompt, max_tokens=2048, temperature=0.2, top_p=0.9, repeat_penalty=1.1, stream=True):
|
|
|
|
|
|
|
|
|
|
| 62 |
output += token["choices"][0]["text"]
|
| 63 |
yield output
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# ----------------------------
|
| 66 |
# FastAPI API
|
| 67 |
# ----------------------------
|
|
@@ -87,22 +124,31 @@ def chat_endpoint(request: ChatRequest):
|
|
| 87 |
# Gradio UI
|
| 88 |
# ----------------------------
|
| 89 |
with gr.Blocks(theme=gr.Theme.from_hub("JackismyShephard/ultimate-rvc-theme")) as demo:
|
| 90 |
-
gr.HTML("<h2 style='text-align:center; color:white;'>
|
| 91 |
-
|
| 92 |
-
chatbot = gr.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# Rounded corners for main container
|
| 99 |
demo.css = """
|
| 100 |
-
.gradio-container {
|
| 101 |
-
border-radius: 25px !important;
|
| 102 |
-
max-width: 600px !important;
|
| 103 |
-
margin: auto !important;
|
| 104 |
-
overflow: hidden;
|
| 105 |
-
}
|
| 106 |
.message.user { border-radius: 18px 18px 4px 18px !important; }
|
| 107 |
.message.bot { border-radius: 18px 18px 18px 4px !important; }
|
| 108 |
"""
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from fastapi import FastAPI
|
| 7 |
from pydantic import BaseModel
|
| 8 |
+
from threading import Thread, Event
|
| 9 |
import uvicorn
|
| 10 |
|
| 11 |
+
# ----------------------------
|
| 12 |
+
# STOP EVENT
|
| 13 |
+
# ----------------------------
|
| 14 |
+
stop_event = Event()
|
| 15 |
+
|
| 16 |
# ----------------------------
|
| 17 |
# Model
|
| 18 |
# ----------------------------
|
|
|
|
| 36 |
# System Prompt
|
| 37 |
# ----------------------------
|
| 38 |
SYSTEM_PROMPT = """
|
| 39 |
+
You are an advanced AI assistant.
|
| 40 |
+
Answer questions clearly and concisely.
|
| 41 |
+
You can handle multi-turn conversations and provide detailed responses if needed.
|
| 42 |
"""
|
| 43 |
|
| 44 |
# ----------------------------
|
|
|
|
| 50 |
|
| 51 |
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 52 |
for h in history:
|
| 53 |
+
if isinstance(h, dict):
|
| 54 |
role = h["role"]
|
| 55 |
msg = h["message"]
|
| 56 |
+
prompt += f"<|im_start|>{role}\n{msg}<|im_end|>\n"
|
| 57 |
+
elif isinstance(h, (list, tuple)) and len(h) == 2:
|
| 58 |
+
u, a = h
|
|
|
|
|
|
|
|
|
|
| 59 |
prompt += f"<|im_start|>user\n{u}<|im_end|>\n<|im_start|>assistant\n{a}<|im_end|>\n"
|
| 60 |
|
| 61 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 62 |
|
| 63 |
output = ""
|
| 64 |
for token in llm(prompt, max_tokens=2048, temperature=0.2, top_p=0.9, repeat_penalty=1.1, stream=True):
|
| 65 |
+
if stop_event.is_set():
|
| 66 |
+
stop_event.clear()
|
| 67 |
+
break
|
| 68 |
output += token["choices"][0]["text"]
|
| 69 |
yield output
|
| 70 |
|
| 71 |
+
# ----------------------------
|
| 72 |
+
# COPY / DOWNLOAD / EDIT FUNCTIONS
|
| 73 |
+
# ----------------------------
|
| 74 |
+
def copy_last(history):
|
| 75 |
+
if history:
|
| 76 |
+
last = history[-1]
|
| 77 |
+
if isinstance(last, dict):
|
| 78 |
+
return last["message"]
|
| 79 |
+
return last[1]
|
| 80 |
+
return ""
|
| 81 |
+
|
| 82 |
+
def download_history(history):
|
| 83 |
+
file_path = "chat_history.txt"
|
| 84 |
+
text = ""
|
| 85 |
+
for h in history:
|
| 86 |
+
if isinstance(h, dict):
|
| 87 |
+
text += f'{h["role"]}: {h["message"]}\n\n'
|
| 88 |
+
else:
|
| 89 |
+
text += f'User: {h[0]}\nAI: {h[1]}\n\n'
|
| 90 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 91 |
+
f.write(text)
|
| 92 |
+
return file_path
|
| 93 |
+
|
| 94 |
+
def edit_message(history, index, new_text):
|
| 95 |
+
if index < len(history):
|
| 96 |
+
if isinstance(history[index], dict):
|
| 97 |
+
history[index]["message"] = new_text
|
| 98 |
+
else:
|
| 99 |
+
history[index] = (new_text, history[index][1])
|
| 100 |
+
return history
|
| 101 |
+
|
| 102 |
# ----------------------------
|
| 103 |
# FastAPI API
|
| 104 |
# ----------------------------
|
|
|
|
| 124 |
# Gradio UI
|
| 125 |
# ----------------------------
|
| 126 |
with gr.Blocks(theme=gr.Theme.from_hub("JackismyShephard/ultimate-rvc-theme")) as demo:
|
| 127 |
+
gr.HTML("<h2 style='text-align:center; color:white;'>Advanced AI Chat</h2>")
|
| 128 |
+
|
| 129 |
+
chatbot = gr.Chatbot(height=600)
|
| 130 |
+
msg = gr.Textbox(placeholder="Type your question...", container=False)
|
| 131 |
+
state = gr.State([])
|
| 132 |
+
|
| 133 |
+
send_btn = gr.Button("Send")
|
| 134 |
+
stop_btn = gr.Button("π Stop")
|
| 135 |
+
copy_btn = gr.Button("π Copy Last")
|
| 136 |
+
download_btn = gr.File(label="π₯ Download Chat")
|
| 137 |
+
edit_index = gr.Number(label="Edit Message Index")
|
| 138 |
+
edit_text = gr.Textbox(label="New Text")
|
| 139 |
+
edit_btn = gr.Button("βοΈ Edit")
|
| 140 |
+
|
| 141 |
+
# ----------------------------
|
| 142 |
+
# Button callbacks
|
| 143 |
+
# ----------------------------
|
| 144 |
+
send_btn.click(generate_response, [msg, state], [chatbot, state])
|
| 145 |
+
stop_btn.click(lambda: stop_event.set())
|
| 146 |
+
copy_btn.click(copy_last, inputs=[state], outputs=None)
|
| 147 |
+
download_btn.click(download_history, inputs=[state], outputs=download_btn)
|
| 148 |
+
edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
|
| 149 |
|
|
|
|
| 150 |
demo.css = """
|
| 151 |
+
.gradio-container { border-radius: 25px !important; max-width: 600px !important; margin:auto !important; overflow:hidden; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
.message.user { border-radius: 18px 18px 4px 18px !important; }
|
| 153 |
.message.bot { border-radius: 18px 18px 18px 4px !important; }
|
| 154 |
"""
|