Cartof commited on
Commit
b04f907
1 Parent(s): af0e6f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -69
app.py CHANGED
@@ -1,9 +1,5 @@
1
  import gradio as gr
2
  import openai
3
- import os
4
-
5
- current_dir = os.path.dirname(os.path.abspath(__file__))
6
- css_file = os.path.join(current_dir, "style.css")
7
 
8
  initial_prompt = "You are a helpful assistant."
9
 
@@ -25,30 +21,34 @@ def parse_text(text):
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
 
@@ -58,9 +58,9 @@ 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):
@@ -70,26 +70,11 @@ def delete_last_conversation(chatbot, context):
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
  title = """<h1 align="center">Tu întrebi și eu răspund.</h1>"""
95
  description = """<div align=center>
@@ -99,40 +84,15 @@ Will not describe your needs to ChatGPT?You Use [ChatGPT Shortcut](https://new
99
  </div>
100
  """
101
 
102
- with gr.Blocks() as demo:
103
- gr.HTML(title)
104
- chatbot = gr.Chatbot().style(color_map=("#A238FF", "#A238FF"))
105
- context = gr.State([])
106
- systemPrompt = gr.State(update_system(initial_prompt))
107
-
108
- with gr.Row():
109
- with gr.Column(scale=12):
110
- txt = gr.Textbox(show_label=False, placeholder="Please enter any of your needs here").style(container=False)
111
- with gr.Column(min_width=50, scale=1):
112
- #submitBtn = gr.Button("🚀 Submit", variant="Primary")
113
- submitBtn = gr.Button("🚀 Submit").style(css={"background-color": "#A238FF"})
114
- with gr.Row():
115
- emptyBtn = gr.Button("🧹 New conversation")
116
- retryBtn = gr.Button("🔄 Resubmit")
117
- delLastBtn = gr.Button("🗑️ Delete conversation")
118
- #reduceTokenBtn = gr.Button("♻️ Optimize Tokens")
119
-
120
- #newSystemPrompt = gr.Textbox(show_label=True, placeholder=f"Setting System Prompt...", label="Change System prompt").style(container=True)
121
- #systemPromptDisplay = gr.Textbox(show_label=True, value=initial_prompt, interactive=False, label="Current System prompt").style(container=True)
122
-
123
- #gr.Markdown(description)
124
-
125
- txt.submit(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
126
- txt.submit(lambda :"", None, txt)
127
- submitBtn.click(predict, [chatbot, txt, systemPrompt, context], [chatbot, context], show_progress=True)
128
- submitBtn.click(lambda :"", None, txt)
129
- emptyBtn.click(reset_state, outputs=[chatbot, context])
130
- #newSystemPrompt.submit(update_system, newSystemPrompt, systemPrompt)
131
- #newSystemPrompt.submit(lambda x: x, newSystemPrompt, systemPromptDisplay)
132
- #newSystemPrompt.submit(lambda :"", None, newSystemPrompt)
133
- retryBtn.click(retry, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
134
- delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
135
- #reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context], [chatbot, context], show_progress=True)
136
-
137
-
138
- demo.launch()
 
1
  import gradio as gr
2
  import openai
 
 
 
 
3
 
4
  initial_prompt = "You are a helpful assistant."
5
 
 
21
 
22
  def get_response(system, context, raw = False):
23
  openai.api_key = "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"
24
+ response = openai.Completion.create(
25
+ engine="text-davinci-002",
26
+ prompt=f"{system}\n\n{context}",
27
+ temperature=0.7,
28
+ max_tokens=1024,
29
+ top_p=1,
30
+ frequency_penalty=0,
31
+ presence_penalty=0,
32
+ stop=None,
33
  )
34
  if raw:
35
  return response
36
  else:
37
+ statistics = f'This conversation Tokens usage【{response["total_characters"]} / 2048】 ( Question + above {response["prompt"]["length"]},Answer {response["choices"][0]["length"]} )'
38
+ message = response.choices[0].text
39
 
40
+ message_with_stats = f'{message}\n\n{statistics}'
 
41
 
42
  return message, parse_text(message_with_stats)
 
43
 
44
  def predict(chatbot, input_sentence, system, context):
45
  if len(input_sentence) == 0:
46
  return []
47
+ context.append(input_sentence)
48
 
49
  message, message_with_stats = get_response(system, context)
50
 
51
+ context.append(message)
52
 
53
  chatbot.append((input_sentence, message_with_stats))
54
 
 
58
  if len(context) == 0:
59
  return [], []
60
  message, message_with_stats = get_response(system, context[:-1])
61
+ context[-1] = message
62
 
63
+ chatbot[-1] = (context[-2], message_with_stats)
64
  return chatbot, context
65
 
66
  def delete_last_conversation(chatbot, context):
 
70
  context = context[:-2]
71
  return chatbot, context
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  def reset_state():
74
  return [], []
75
 
76
  def update_system(new_system_prompt):
77
+ return new_system_prompt
78
 
79
  title = """<h1 align="center">Tu întrebi și eu răspund.</h1>"""
80
  description = """<div align=center>
 
84
  </div>
85
  """
86
 
87
+ gr.Interface(
88
+ predict,
89
+ inputs=["textbox", "text", "text", "text"],
90
+ outputs="text",
91
+ title=title,
92
+ description=description,
93
+ allow_flagging=False,
94
+ theme="huggingface",
95
+ layout="vertical",
96
+ examples=[["What's your name?", "I don't have a name. How can I assist you today?", "", ""]],
97
+ interpretation="default"
98
+ ).launch()