Cartof commited on
Commit
a00c189
1 Parent(s): 05c4232

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -138
app.py CHANGED
@@ -1,138 +1,29 @@
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
-
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
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Chatbot Demo</title>
6
+ <link rel="stylesheet" href="style.css">
7
+ </head>
8
+ <body>
9
+ <h1>Gradio Chatbot Demo</h1>
10
+ <div>Introduceți o întrebare și primiți un răspuns</div>
11
+ <form>
12
+ <input type="text" id="message" name="message" placeholder="Scrieți un mesaj...">
13
+ <button type="submit">Trimite</button>
14
+ <button type="reset">Resetați</button>
15
+ <button type="retry" style="display:none">Reîncercați</button>
16
+ <button type="deleteLast">Ștergeți ultimul mesaj</button>
17
+ </form>
18
+ <div class="gradio-chatbot">
19
+ </div>
20
+ <script src="https://cdn.gradio.app/js/chatbot/v1.0.0/chatbot.js"></script>
21
+ <script>
22
+ var chatbot = new GradioChatbot({
23
+ "type": "web",
24
+ "botId": "put your bot id here",
25
+ "serverName": "put your server name here"
26
+ });
27
+ </script>
28
+ </body>
29
+ </html>