peterkchung commited on
Commit
4879b2a
·
verified ·
1 Parent(s): e6d3d50

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Chat engine.
3
+
4
+ TODOs:
5
+ - Better prompts.
6
+ - Output reader / parser.
7
+ - Agents for evaluation and task planning / splitting.
8
+ * Haystack for orchestration
9
+ - Tools for agents
10
+ * Haystack for orchestration
11
+ -
12
+
13
+ """
14
+
15
+
16
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
17
+
18
+ def query_submit(user_message, history):
19
+ return "", history + [[user_message, None]]
20
+
21
+ def format_prompt(query, history, lookback):
22
+ prompt = "Responses should be no more than 100 words long.\n"
23
+
24
+ for previous_query, prevous_completion in history[-lookback:]:
25
+ prompt += f"<s>[INST] {previous_query} [/INST] {prevous_completion}</s> "
26
+
27
+ prompt += f"[INST] {query} [/INST]"
28
+
29
+ return prompt
30
+
31
+ def query_completion(
32
+ query,
33
+ history,
34
+ lookback = 3,
35
+ max_new_tokens = 256,
36
+ ):
37
+
38
+ generateKwargs = dict(
39
+ max_new_tokens = max_new_tokens,
40
+ seed = 1337,
41
+ )
42
+
43
+ formatted_query = format_prompt(query, history, lookback)
44
+
45
+ stream = client.text_generation(
46
+ formatted_query,
47
+ **generateKwargs,
48
+ stream = True,
49
+ details = True,
50
+ return_full_text = False
51
+ )
52
+
53
+ history[-1][1] = ""
54
+
55
+ for response in stream:
56
+ history[-1][1] += response.token.text
57
+ yield history
58
+
59
+
60
+ """
61
+ Chat UI using Gradio Blocks.
62
+
63
+ Blocks preferred for lower-level "atomic" layout control and state management.
64
+
65
+ TODOs:
66
+ - State management for dynamic components update.
67
+ - Add scratpad readout to right of chat log.
68
+ * Placeholder added for now.
69
+ - Add functionality to retry button.
70
+ * Placeholder added for now.
71
+ - Add dropdown for model selection.
72
+ - Add textbox for HF model selection.
73
+
74
+ """
75
+
76
+ with gr.Blocks() as chatUI:
77
+ with gr.Row():
78
+ chatOutput = gr.Chatbot(
79
+ bubble_full_width = False,
80
+ scale = 2
81
+ )
82
+ agentWhiteBoard = gr.Markdown(scale = 1)
83
+
84
+ with gr.Row():
85
+ queryInput = gr.Textbox(
86
+ placeholder = "Please enter you question or request here...",
87
+ show_label = False,
88
+ scale = 4
89
+ )
90
+ submitButton = gr.Button("Submit", scale = 1)
91
+
92
+ with gr.Row():
93
+ retry = gr.Button("Retry (null)")
94
+ clear = gr.ClearButton([queryInput, chatOutput])
95
+
96
+ # gr.State()
97
+
98
+ queryInput.submit(
99
+ fn = query_submit,
100
+ inputs = [queryInput, chatOutput],
101
+ outputs = [queryInput, chatOutput],
102
+ queue = False,
103
+ ).then(
104
+ fn = query_completion,
105
+ inputs = [queryInput, chatOutput],
106
+ outputs = [chatOutput],
107
+ )
108
+
109
+ submitButton.click(
110
+ fn = query_submit,
111
+ inputs = [queryInput, chatOutput],
112
+ outputs = [queryInput, chatOutput],
113
+ queue = False,
114
+ ).then(
115
+ fn = query_completion,
116
+ inputs = [queryInput, chatOutput],
117
+ outputs = [chatOutput],
118
+ )
119
+
120
+ chatUI.queue()
121
+ chatUI.launch(show_api = False)