Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def http_yield(prompt):
|
| 9 |
+
print(prompt)
|
| 10 |
+
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
| 11 |
+
for character in bot_message:
|
| 12 |
+
yield character
|
| 13 |
+
|
| 14 |
+
def http_bot_yield(prompt):
|
| 15 |
+
headers = {"User-Agent": "vLLM Client"}
|
| 16 |
+
pload = {
|
| 17 |
+
"prompt": f"<s>[INST] {prompt} [/INST] ",
|
| 18 |
+
"stream": True,
|
| 19 |
+
"max_tokens": 256,
|
| 20 |
+
"temperature": 0.1
|
| 21 |
+
}
|
| 22 |
+
response = requests.post(
|
| 23 |
+
'https://ca21-34-126-145-53.ngrok-free.app/generate',
|
| 24 |
+
headers=headers,
|
| 25 |
+
json=pload,
|
| 26 |
+
stream=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
for chunk in response.iter_lines(chunk_size=8192,
|
| 30 |
+
decode_unicode=False,
|
| 31 |
+
delimiter=b"\0"):
|
| 32 |
+
if chunk:
|
| 33 |
+
data = json.loads(chunk.decode("utf-8"))
|
| 34 |
+
output = data["text"][0].split('[/INST] ')[-1]
|
| 35 |
+
# print("output --->", output)
|
| 36 |
+
yield output
|
| 37 |
+
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
chatbot = gr.Chatbot()
|
| 40 |
+
msg = gr.Textbox()
|
| 41 |
+
clear = gr.Button("Clear")
|
| 42 |
+
|
| 43 |
+
def user(user_message, history):
|
| 44 |
+
print("", history + [[user_message, None]])
|
| 45 |
+
return "", history + [[user_message, None]]
|
| 46 |
+
|
| 47 |
+
def bot(history):
|
| 48 |
+
print(history[-1][0])
|
| 49 |
+
prompt = history[-1][0]
|
| 50 |
+
history[-1][1] = ""
|
| 51 |
+
# b_text = ""
|
| 52 |
+
print(history)
|
| 53 |
+
for character in http_bot_yield(prompt):
|
| 54 |
+
# print("yld --- > ", history[-1][1])
|
| 55 |
+
history[-1][1] = character
|
| 56 |
+
# b_text += character
|
| 57 |
+
# print("yield --- > ", b_text)
|
| 58 |
+
time.sleep(0.05)
|
| 59 |
+
yield history
|
| 60 |
+
|
| 61 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 62 |
+
bot, chatbot, chatbot
|
| 63 |
+
)
|
| 64 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 65 |
+
|
| 66 |
+
demo.queue()
|
| 67 |
+
demo.launch()
|