Cartof commited on
Commit
afc461f
1 Parent(s): 6f738f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -131
app.py CHANGED
@@ -1,138 +1,54 @@
1
  import gradio as gr
2
  import openai
3
  import os
4
- import sys
5
- import kivy
6
- # import markdown
7
 
8
  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] = 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
- title = """<h1 align="center">Tu întrebi și eu răspund.</h1>"""
95
- description = """<div align=center>
96
-
97
- Will not describe your needs to ChatGPT?You Use [ChatGPT Shortcut](https://newzone.top/chatgpt/)
98
-
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
  import os
4
+
5
+ css_file = os.path.join(os.path.dirname(file), "style.css")
 
6
 
7
  initial_prompt = "You are a helpful assistant."
8
 
9
  def parse_text(text):
10
+ lines = text.split("\n")
11
+ for i,line in enumerate(lines):
12
+ if "" in line: items = line.split('`') if items[-1]: lines[i] = f'<pre><code class="{items[-1]}">' else: lines[i]= '<pre><code>' elif "" in line and "</code></pre>" not in line:
13
+ lines[i] = "</code></pre>"
14
+ elif "#" in line:
15
+ lines[i] = f"<h1>{line.strip('#')}</h1>"
16
+ elif "##" in line:
17
+ lines[i] = f"<h2>{line.strip('##')}</h2>"
18
+ elif "###" in line:
19
+ lines[i] = f"<h3>{line.strip('###')}</h3>"
20
+ return "\n".join(lines)
21
+
22
+ def generate_response(prompt):
23
+ # procesăm promptul
24
+ prompt = parse_text(prompt)
25
+ # configurăm OpenAI API
26
+ openai.api_key = "API_KEY_HERE"
27
+ model_engine = "davinci"
28
+ prompt_length = len(prompt)
29
+ max_tokens = 1024 - prompt_length
30
+ temperature = 0.7
31
+ # generăm răspunsul folosind OpenAI API
32
+ response = openai.Completion.create(
33
+ engine=model_engine,
34
+ prompt=prompt,
35
+ max_tokens=max_tokens,
36
+ n=1,
37
+ temperature=temperature,
38
+ )
39
+ message = response.choices[0].text.strip()
40
+ # procesăm răspunsul generat
41
+ message = parse_text(message)
42
+ return message
43
+
44
+ definim interfața Gradio
45
+ iface = gr.Interface(
46
+ fn=generate_response,
47
+ inputs=gr.inputs.Textbox(lines=7, label="Input prompt"),
48
+ outputs=gr.outputs.HTML(label="Model response"),
49
+ title="OpenAI GPT-3 assistant",
50
+ layout="vertical",
51
+ width=800,
52
+ css=css_file,
53
+ )
54
+ iface.launch()