daniloedu commited on
Commit
044cc98
·
1 Parent(s): 26f28ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -42
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 query(payload):
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
- response = query({"inputs": prompt})
25
- assistant_message = response['generated_text'].split("Assistant:")[-1].strip()
26
- chat_history[-1] = (message, assistant_message)
27
- return "", chat_history
28
-
29
- def clear_inputs(input1, input2, input3):
30
- input1.value = ""
31
- input2.value = []
32
- input3.value = "A conversation between a user and an AI assistant. The assistant gives helpful and honest answers."
33
- return input1, input2, input3
34
-
35
- iface = gr.Interface(
36
- fn=respond,
37
- inputs=[
38
- Textbox(label="Prompt"),
39
- Chat(label="Chat History", height=240),
40
- Textbox(label="System message", lines=2, default="A conversation between a user and an AI assistant. The assistant gives helpful and honest answers.")
41
- ],
42
- outputs=[
43
- Textbox(label="Prompt"),
44
- Chat(label="Chat History", height=240),
45
- Textbox(label="System message")
46
- ],
47
- server_name="0.0.0.0",
48
- server_port=7860,
49
- allow_flagging=False,
50
- title="Chat with AI",
51
- description="This is a chat interface that connects to a language model.",
52
- analytics_enabled=False,
53
- )
54
-
55
- iface.add_button("Clear", clear_inputs)
56
- iface.add_button("Submit", respond)
57
-
58
- iface.launch()
 
 
 
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