Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,6 @@ 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 |
# ----------------------------
|
|
@@ -32,22 +29,17 @@ llm = Llama(
|
|
| 32 |
|
| 33 |
llm("warmup", max_tokens=1)
|
| 34 |
|
| 35 |
-
# ----------------------------
|
| 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 |
# ----------------------------
|
| 45 |
-
# Chat
|
| 46 |
# ----------------------------
|
| 47 |
def generate_response(message, history):
|
| 48 |
yield "π€ Thinking..."
|
| 49 |
-
time.sleep(0.
|
| 50 |
-
|
| 51 |
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 52 |
for h in history:
|
| 53 |
if isinstance(h, dict):
|
|
@@ -59,7 +51,6 @@ def generate_response(message, history):
|
|
| 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():
|
|
@@ -68,9 +59,6 @@ def generate_response(message, history):
|
|
| 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]
|
|
@@ -89,7 +77,7 @@ def download_history(history):
|
|
| 89 |
file_path = "chat_history.txt"
|
| 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):
|
|
@@ -103,7 +91,6 @@ def edit_message(history, index, new_text):
|
|
| 103 |
# FastAPI API
|
| 104 |
# ----------------------------
|
| 105 |
app = FastAPI()
|
| 106 |
-
|
| 107 |
class ChatRequest(BaseModel):
|
| 108 |
message: str
|
| 109 |
history: list = []
|
|
@@ -115,7 +102,6 @@ def chat_endpoint(request: ChatRequest):
|
|
| 115 |
for u, a in request.history:
|
| 116 |
prompt += f"<|im_start|>user\n{u}<|im_end|>\n<|im_start|>assistant\n{a}<|im_end|>\n"
|
| 117 |
prompt += f"<|im_start|>user\n{request.message}<|im_end|>\n<|im_start|>assistant\n"
|
| 118 |
-
|
| 119 |
for token in llm(prompt, max_tokens=2048, temperature=0.2, top_p=0.9, repeat_penalty=1.1, stream=True):
|
| 120 |
output += token["choices"][0]["text"]
|
| 121 |
return {"response": output}
|
|
@@ -124,22 +110,21 @@ def chat_endpoint(request: ChatRequest):
|
|
| 124 |
# Gradio UI
|
| 125 |
# ----------------------------
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
-
with gr.
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
edit_btn = gr.Button("βοΈ Edit")
|
| 143 |
|
| 144 |
# ----------------------------
|
| 145 |
# Callbacks
|
|
@@ -147,7 +132,7 @@ with gr.Blocks() as demo:
|
|
| 147 |
send_btn.click(generate_response, [msg, state], [chatbot, state])
|
| 148 |
stop_btn.click(lambda: stop_event.set())
|
| 149 |
copy_btn.click(copy_last, inputs=[state], outputs=None)
|
| 150 |
-
|
| 151 |
edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
|
| 152 |
|
| 153 |
demo.css = """
|
|
@@ -165,9 +150,6 @@ with gr.Blocks() as demo:
|
|
| 165 |
.message.bot { border-radius: 18px 18px 18px 4px !important; background:#1c1f2a !important; color:white !important;}
|
| 166 |
"""
|
| 167 |
|
| 168 |
-
# ----------------------------
|
| 169 |
-
# Run Gradio + FastAPI
|
| 170 |
-
# ----------------------------
|
| 171 |
def run_gradio():
|
| 172 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 173 |
|
|
|
|
| 8 |
from threading import Thread, Event
|
| 9 |
import uvicorn
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
stop_event = Event()
|
| 12 |
|
| 13 |
# ----------------------------
|
|
|
|
| 29 |
|
| 30 |
llm("warmup", max_tokens=1)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
SYSTEM_PROMPT = """
|
| 33 |
+
You are an advanced AI assistant.
|
| 34 |
+
Answer questions clearly and concisely.
|
|
|
|
| 35 |
"""
|
| 36 |
|
| 37 |
# ----------------------------
|
| 38 |
+
# Chat function
|
| 39 |
# ----------------------------
|
| 40 |
def generate_response(message, history):
|
| 41 |
yield "π€ Thinking..."
|
| 42 |
+
time.sleep(0.3)
|
|
|
|
| 43 |
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 44 |
for h in history:
|
| 45 |
if isinstance(h, dict):
|
|
|
|
| 51 |
prompt += f"<|im_start|>user\n{u}<|im_end|>\n<|im_start|>assistant\n{a}<|im_end|>\n"
|
| 52 |
|
| 53 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
| 54 |
output = ""
|
| 55 |
for token in llm(prompt, max_tokens=2048, temperature=0.2, top_p=0.9, repeat_penalty=1.1, stream=True):
|
| 56 |
if stop_event.is_set():
|
|
|
|
| 59 |
output += token["choices"][0]["text"]
|
| 60 |
yield output
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
def copy_last(history):
|
| 63 |
if history:
|
| 64 |
last = history[-1]
|
|
|
|
| 77 |
file_path = "chat_history.txt"
|
| 78 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 79 |
f.write(text)
|
| 80 |
+
return file_path # return path for gr.File output
|
| 81 |
|
| 82 |
def edit_message(history, index, new_text):
|
| 83 |
if index < len(history):
|
|
|
|
| 91 |
# FastAPI API
|
| 92 |
# ----------------------------
|
| 93 |
app = FastAPI()
|
|
|
|
| 94 |
class ChatRequest(BaseModel):
|
| 95 |
message: str
|
| 96 |
history: list = []
|
|
|
|
| 102 |
for u, a in request.history:
|
| 103 |
prompt += f"<|im_start|>user\n{u}<|im_end|>\n<|im_start|>assistant\n{a}<|im_end|>\n"
|
| 104 |
prompt += f"<|im_start|>user\n{request.message}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
| 105 |
for token in llm(prompt, max_tokens=2048, temperature=0.2, top_p=0.9, repeat_penalty=1.1, stream=True):
|
| 106 |
output += token["choices"][0]["text"]
|
| 107 |
return {"response": output}
|
|
|
|
| 110 |
# Gradio UI
|
| 111 |
# ----------------------------
|
| 112 |
with gr.Blocks() as demo:
|
| 113 |
+
with gr.Column():
|
| 114 |
+
chatbot = gr.Chatbot(height=700)
|
| 115 |
+
state = gr.State([])
|
| 116 |
+
msg = gr.Textbox(placeholder="Type your message...", container=False)
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
send_btn = gr.Button("Send")
|
| 120 |
+
stop_btn = gr.Button("π Stop")
|
| 121 |
+
copy_btn = gr.Button("π Copy Last")
|
| 122 |
+
download_file = gr.File(label="Download Chat", file_types=[".txt"])
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
edit_index = gr.Number(label="Edit Index")
|
| 126 |
+
edit_text = gr.Textbox(label="New Text")
|
| 127 |
+
edit_btn = gr.Button("βοΈ Edit")
|
|
|
|
| 128 |
|
| 129 |
# ----------------------------
|
| 130 |
# Callbacks
|
|
|
|
| 132 |
send_btn.click(generate_response, [msg, state], [chatbot, state])
|
| 133 |
stop_btn.click(lambda: stop_event.set())
|
| 134 |
copy_btn.click(copy_last, inputs=[state], outputs=None)
|
| 135 |
+
download_file.output = download_history # assign output function
|
| 136 |
edit_btn.click(edit_message, inputs=[state, edit_index, edit_text], outputs=[chatbot])
|
| 137 |
|
| 138 |
demo.css = """
|
|
|
|
| 150 |
.message.bot { border-radius: 18px 18px 18px 4px !important; background:#1c1f2a !important; color:white !important;}
|
| 151 |
"""
|
| 152 |
|
|
|
|
|
|
|
|
|
|
| 153 |
def run_gradio():
|
| 154 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 155 |
|