Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio.components import Textbox, Chat
|
| 3 |
|
| 4 |
load_dotenv()
|
| 5 |
|
|
@@ -14,47 +14,45 @@ def format_chat_prompt(message, chat_history, instruction):
|
|
| 14 |
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 15 |
return prompt
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 19 |
-
return response.json()
|
| 20 |
-
|
| 21 |
-
def respond(message, chat_history=[], instruction="A conversation between a user and an AI assistant. The assistant gives helpful and honest answers."):
|
| 22 |
-
chat_history.append((message, ""))
|
| 23 |
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio.components import Textbox, Chat, Slider
|
| 3 |
|
| 4 |
load_dotenv()
|
| 5 |
|
|
|
|
| 14 |
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
| 15 |
return prompt
|
| 16 |
|
| 17 |
+
def respond(message, chat_history, instruction, temperature=0.7):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 19 |
+
chat_history = chat_history + [[message, ""]]
|
| 20 |
+
stream = client.generate_stream(prompt,
|
| 21 |
+
max_new_tokens=1024,
|
| 22 |
+
stop_sequences=["\nUser:", "<|endoftext|>"],
|
| 23 |
+
temperature=temperature)
|
| 24 |
+
#stop_sequences to not generate the user answer
|
| 25 |
+
acc_text = ""
|
| 26 |
+
#Streaming the tokens
|
| 27 |
+
for idx, response in enumerate(stream):
|
| 28 |
+
text_token = response.token.text
|
| 29 |
+
|
| 30 |
+
if response.details:
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
if idx == 0 and text_token.startswith(" "):
|
| 34 |
+
text_token = text_token[1:]
|
| 35 |
+
|
| 36 |
+
acc_text += text_token
|
| 37 |
+
last_turn = list(chat_history.pop(-1))
|
| 38 |
+
last_turn[-1] += acc_text
|
| 39 |
+
chat_history = chat_history + [last_turn]
|
| 40 |
+
yield "", chat_history
|
| 41 |
+
acc_text = ""
|
| 42 |
+
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
| 45 |
+
msg = gr.Textbox(label="Prompt")
|
| 46 |
+
with gr.Accordion(label="Advanced options",open=False):
|
| 47 |
+
system = gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.")
|
| 48 |
+
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
| 49 |
+
btn = gr.Button("Submit")
|
| 50 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 51 |
+
|
| 52 |
+
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
| 53 |
+
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot]) #Press enter to submit
|
| 54 |
+
gr.close_all()
|
| 55 |
+
demo.queue().launch(share=True, server_port=int(os.environ['PORT4']))
|
| 56 |
+
|
| 57 |
|
| 58 |
|