Update app.py
Browse files
app.py
CHANGED
@@ -25,45 +25,57 @@ LICENSE = """
|
|
25 |
--- Apache 2.0 License ---
|
26 |
"""
|
27 |
|
28 |
-
def
|
|
|
|
|
|
|
29 |
"""Generate a response using the Llama model."""
|
30 |
-
|
|
|
|
|
31 |
response = model.create_chat_completion(
|
32 |
-
messages=[{"role": "user", "content": message}],
|
33 |
temperature=temperature,
|
34 |
max_tokens=max_tokens,
|
35 |
top_p=top_p,
|
36 |
stream=True,
|
37 |
)
|
|
|
|
|
38 |
for streamed in response:
|
39 |
delta = streamed["choices"][0].get("delta", {})
|
40 |
text_chunk = delta.get("content", "")
|
41 |
-
|
42 |
-
yield
|
43 |
|
44 |
with gr.Blocks() as demo:
|
45 |
gr.Markdown(DESCRIPTION)
|
46 |
|
47 |
-
chatbot = gr.
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
examples=[
|
52 |
["How many r's are in the word strawberry?"],
|
53 |
['How to stop a cough?'],
|
54 |
['How do I relieve feet pain?'],
|
55 |
],
|
56 |
-
|
57 |
-
|
58 |
-
fill_width=True
|
59 |
)
|
60 |
|
61 |
-
|
62 |
-
# gr.Slider(minimum=512, maximum=4096, value=1024, step=1, label="Max Tokens")
|
63 |
-
# gr.Slider(minimum=0.1, maximum=1.5, value=0.9, step=0.1, label="Temperature")
|
64 |
-
# gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
65 |
-
|
66 |
-
#gr.Markdown(LICENSE)
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
demo.launch()
|
|
|
25 |
--- Apache 2.0 License ---
|
26 |
"""
|
27 |
|
28 |
+
def user(message, history):
|
29 |
+
return "", history + [{"role": "user", "content": message}]
|
30 |
+
|
31 |
+
def generate_text(history, max_tokens=512, temperature=0.9, top_p=0.95):
|
32 |
"""Generate a response using the Llama model."""
|
33 |
+
messages = [{"role": item["role"], "content": item["content"]} for item in history[:-1]]
|
34 |
+
message = history[-1]['content']
|
35 |
+
|
36 |
response = model.create_chat_completion(
|
37 |
+
messages=messages + [{"role": "user", "content": message}],
|
38 |
temperature=temperature,
|
39 |
max_tokens=max_tokens,
|
40 |
top_p=top_p,
|
41 |
stream=True,
|
42 |
)
|
43 |
+
history.append({"role": "assistant", "content": ""})
|
44 |
+
|
45 |
for streamed in response:
|
46 |
delta = streamed["choices"][0].get("delta", {})
|
47 |
text_chunk = delta.get("content", "")
|
48 |
+
history[-1]['content'] += text_chunk
|
49 |
+
yield history
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
gr.Markdown(DESCRIPTION)
|
53 |
|
54 |
+
chatbot = gr.Chatbot(type="messages")
|
55 |
+
msg = gr.Textbox()
|
56 |
+
clear = gr.Button("Clear")
|
57 |
+
|
58 |
+
with gr.Accordion("Adjust Parameters", open=False):
|
59 |
+
max_tokens = gr.Slider(minimum=512, maximum=4096, value=1024, step=1, label="Max Tokens")
|
60 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.5, value=0.9, step=0.1, label="Temperature")
|
61 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
62 |
+
|
63 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
64 |
+
generate_text, [chatbot, max_tokens, temperature, top_p], chatbot
|
65 |
+
)
|
66 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
67 |
+
|
68 |
+
gr.Examples(
|
69 |
examples=[
|
70 |
["How many r's are in the word strawberry?"],
|
71 |
['How to stop a cough?'],
|
72 |
['How do I relieve feet pain?'],
|
73 |
],
|
74 |
+
inputs=msg,
|
75 |
+
label="Examples",
|
|
|
76 |
)
|
77 |
|
78 |
+
gr.Markdown(LICENSE)
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
demo.launch()
|