ysharma HF staff commited on
Commit
7a42908
1 Parent(s): c33f5b6

create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+
4
+ API_URL = "https://joi-20b.ngrok.io/generate_stream"
5
+
6
+ def predict(inputs, history=[], top_p, temperature, top_k, repetition_penalty):
7
+ if not inputs.startswith("User: "):
8
+ inputs = "User: " + inputs + "\n"
9
+ payload = {
10
+ "inputs": inputs, #"My name is Jane and I",
11
+ "parameters": {
12
+ "details": True,
13
+ "do_sample": True,
14
+ "max_new_tokens": 20,
15
+ "repetition_penalty": 1.03,
16
+ "seed": 0,
17
+ "stop": ["photographer"],
18
+ "temperature": 0.5,
19
+ "top_k": 10,
20
+ "top_p": 0.95
21
+ }
22
+ }
23
+
24
+ headers = {
25
+ 'accept': 'text/event-stream',
26
+ 'Content-Type': 'application/json'
27
+ }
28
+
29
+ history.append(inputs)
30
+ response = requests.post(API_URL, headers=headers, json=payload)
31
+ responses = response.text.split("\n\n")
32
+
33
+ partial_words = ""
34
+ for idx, resp in enumerate(responses):
35
+ if resp[:4] == 'data':
36
+ partial_words = partial_words + json.loads(resp[5:])['token']['text']
37
+ #print(partial_words)
38
+ time.sleep(0.05)
39
+ if idx == 0:
40
+ history.append(" " + partial_words)
41
+ else:
42
+ history[-1] = partial_words
43
+
44
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
45
+
46
+ yield chat, history #resembles {chatbot: chat, state: history}
47
+
48
+ title = """<h1 align="center">Gradio Streaming</h1>"""
49
+ description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
50
+ ```
51
+ User: <utterance>
52
+ Assistant: <utterance>
53
+ User: <utterance>
54
+ Assistant: <utterance>
55
+ ...
56
+ ```
57
+ In this app, you can explore the outputs of the Joi alpha language models.
58
+ """
59
+
60
+ with gr.Blocks(css = "#chatbot {height: 400px, overflow: auto;}") as demo:
61
+ gr.HTML(title)
62
+ inputs = gr.Textbox(placeholder= "Hi my name is Joe.", label= "Type an input and press Enter") #t
63
+ chatbot = gr.Chatbot(elem_id='chatbot') #c
64
+ state = gr.State([]) #s
65
+ b1 = gr.Button()
66
+
67
+ #inputs, top_p, temperature, top_k, repetition_penalty
68
+ with gr.Accordion("Parameters", open=False):
69
+ top_p = gr.Slider( minimum=-0, maximum=1.0, value=0.95, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
70
+ temperature = gr.Slider( minimum=-0, maximum=5.0, value=0.5, step=0.1, interactive=True, label="Temperature",)
71
+ top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
72
+ repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
73
+
74
+ #b1.click(predict, [t,s], [c,s])
75
+ #inputs.submit(predict, [t,s], [c,s])
76
+ inputs.submit( inference_chat, [inputs, state, top_p, temperature, top_k, repetition_penalty,], [chatbot, state],)
77
+ b1.click( inference_chat, [inputs, state, top_p, temperature, top_k, repetition_penalty,], [chatbot, state],)
78
+
79
+ gr.HTML(description)
80
+ demo.queue().launch(debug=True)
81
+