Cartof commited on
Commit
8b58bdb
1 Parent(s): 4cfa023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -44
app.py CHANGED
@@ -9,53 +9,122 @@ initial_prompt = "You are a helpful assistant."
9
 
10
  def parse_text(text):
11
  lines = text.split("\n")
12
- for i, line in enumerate(lines):
13
- if "`" in line:
14
  items = line.split('`')
15
  if items[-1]:
16
  lines[i] = f'<pre><code class="{items[-1]}">'
17
  else:
18
- lines[i] = '<pre><code>'
19
- elif "`" in line and "</code></pre>" not in line:
20
- lines[i] = "</code></pre>"
21
- elif "#" in line:
22
- lines[i] = f"<h1>{line.strip('#')}</h1>"
23
- elif "##" in line:
24
- lines[i] = f"<h2>{line.strip('##')}</h2>"
25
- elif "###" in line:
26
- lines[i] = f"<h3>{line.strip('###')}</h3>"
27
- return "\n".join(lines)
28
-
29
- def generate_response(prompt):
30
- # procesăm promptul
31
- prompt = parse_text(prompt)
32
- # configurăm OpenAI API
33
  openai.api_key = "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"
34
- model_engine = "davinci"
35
- prompt_length = len(prompt)
36
- max_tokens = 1024 - prompt_length
37
- temperature = 0.7
38
- #generăm răspunsul folosind OpenAI API
39
- response = openai.Completion.create(
40
- engine=model_engine,
41
- prompt=prompt,
42
- max_tokens=max_tokens,
43
- n=1,
44
- temperature=temperature,
45
  )
46
- message = response.choices[0].text.strip()
47
- # procesăm răspunsul generat
48
- message = parse_text(message)
49
- return message
50
-
51
- # definim interfața Gradio
52
- iface = gr.Interface(
53
- fn=generate_response,
54
- inputs=gr.inputs.Textbox(lines=7, label="Input prompt"),
55
- outputs=gr.outputs.HTML(label="Model response"),
56
- title="OpenAI GPT-3 assistant",
57
- layout="vertical",
58
- width=800,
59
- css=css_file,
60
- )
61
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def parse_text(text):
11
  lines = text.split("\n")
12
+ for i,line in enumerate(lines):
13
+ if "```" in line:
14
  items = line.split('`')
15
  if items[-1]:
16
  lines[i] = f'<pre><code class="{items[-1]}">'
17
  else:
18
+ lines[i] = f'</code></pre>'
19
+ else:
20
+ if i>0:
21
+ line = line.replace("<", "&lt;")
22
+ line = line.replace(">", "&gt;")
23
+ lines[i] = '<br/>'+line.replace(" ", "&nbsp;")
24
+ return "".join(lines)
25
+
26
+ def get_response(system, context, raw = False):
 
 
 
 
 
 
27
  openai.api_key = "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"
28
+ response = openai.ChatCompletion.create(
29
+ model="gpt-3.5-turbo",
30
+ messages=[system, *context],
 
 
 
 
 
 
 
 
31
  )
32
+ if raw:
33
+ return response
34
+ else:
35
+ statistics = f'This conversation Tokens usage【{response["usage"]["total_tokens"]} / 4096】 ( Question + above {response["usage"]["prompt_tokens"]},Answer {response["usage"]["completion_tokens"]} )'
36
+ message = response["choices"][0]["message"]["content"]
37
+
38
+ message_with_stats = f'{message}'
39
+ # message_with_stats = markdown.markdown(message_with_stats)
40
+
41
+ return message, parse_text(message_with_stats)
42
+ #return message
43
+
44
+ def predict(chatbot, input_sentence, system, context):
45
+ if len(input_sentence) == 0:
46
+ return []
47
+ context.append({"role": "user", "content": f"{input_sentence}"})
48
+
49
+ message, message_with_stats = get_response(system, context)
50
+
51
+ context.append({"role": "assistant", "content": message})
52
+
53
+ chatbot.append((input_sentence, message_with_stats))
54
+
55
+ return chatbot, context
56
+
57
+ def retry(chatbot, system, context):
58
+ if len(context) == 0:
59
+ return [], []
60
+ message, message_with_stats = get_response(system, context[:-1])
61
+ context[-1] = {"role": "assistant", "content": message}
62
+
63
+ chatbot[-1] = (context[-2]["content"], message_with_stats)
64
+ return chatbot, context
65
+
66
+ def delete_last_conversation(chatbot, context):
67
+ if len(context) == 0:
68
+ return [], []
69
+ chatbot = chatbot[:-1]
70
+ context = context[:-2]
71
+ return chatbot, context
72
+
73
+ def reduce_token(chatbot, system, context):
74
+ context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
75
+
76
+ response = get_response(system, context, raw=True)
77
+
78
+ statistics = f'本次对话Tokens用量【{response["usage"]["completion_tokens"]+12+12+8} / 4096】'
79
+ optmz_str = markdown.markdown( f'好的,我们之前聊了:{response["choices"][0]["message"]["content"]}\n\n================\n\n{statistics}' )
80
+ chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
81
+
82
+ context = []
83
+ context.append({"role": "user", "content": "我们之前聊了什么?"})
84
+ context.append({"role": "assistant", "content": f'我们之前聊了:{response["choices"][0]["message"]["content"]}'})
85
+ return chatbot, context
86
+
87
+
88
+ def reset_state():
89
+ return [], []
90
+
91
+ def update_system(new_system_prompt):
92
+ return {"role": "system", "content": new_system_prompt}
93
+
94
+ with gr.Blocks() as demo:
95
+ gr.HTML(title)
96
+ chatbot = gr.Chatbot().style(color_map=("#A238FF", "#A238FF"))
97
+ context = gr.State([])
98
+ systemPrompt = gr.State(update_system(initial_prompt))
99
+
100
+ with gr.Row():
101
+ with gr.Column(scale=12):
102
+ txt = gr.Textbox(show_label=False, placeholder="Please enter any of your needs here").style(container=False)
103
+ with gr.Column(min_width=50, scale=1):
104
+ #submitBtn = gr.Button("🚀 Submit", variant="Primary")
105
+ submitBtn = gr.Button("🚀 Submit").style(css={"background-color": "#A238FF"})
106
+ with gr.Row():
107
+ emptyBtn = gr.Button("🧹 New conversation")
108
+ retryBtn = gr.Button("🔄 Resubmit")
109
+ delLastBtn = gr.Button("🗑️ Delete conversation")
110
+ #reduceTokenBtn = gr.Button("♻️ Optimize Tokens")
111
+
112
+ #newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"Setting System Prompt...", label="Change System prompt").style(container=True)
113
+ #systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="Current System prompt").style(container=True)
114
+
115
+ #gr.Markdown(description)
116
+
117
+ txt.submit(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
118
+ txt.submit(lambda :"", None, txt)
119
+ submitBtn.click(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
120
+ submitBtn.click(lambda :"", None, txt)
121
+ emptyBtn.click(reset_state, outputs=[chatbot, context])
122
+ #newSystemPrompt.submit(update_system, newSystemPrompt, systemPrompt)
123
+ #newSystemPrompt.submit(lambda x: x, newSystemPrompt, systemPromptDisplay)
124
+ #newSystemPrompt.submit(lambda :"", None, newSystemPrompt)
125
+ retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
126
+ delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
127
+ #reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
128
+
129
+
130
+ demo.launch()