SamSamSamChennn commited on
Commit
32506e1
·
1 Parent(s): c4e3ccc

[Sam] feat: Add Demo Code

Browse files
Files changed (5) hide show
  1. README.md +5 -4
  2. app.py +58 -0
  3. chatbot_03.ipynb +148 -0
  4. chatbot_03_prompt.md +3 -0
  5. requirements.txt +1 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Ai Test
3
- emoji: 🏆
4
- colorFrom: indigo
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.35.2
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Chatbot Exercise
3
+ emoji: 📊
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.35.2
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ import gradio as gr
4
+
5
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
6
+
7
+ class Conversation:
8
+ def __init__(self, prompt, num_of_round):
9
+ self.prompt = prompt
10
+ self.num_of_round = num_of_round
11
+ self.messages = []
12
+ self.messages.append({"role": "system", "content": self.prompt})
13
+
14
+ def ask(self, question):
15
+ try:
16
+ self.messages.append( {"role": "user", "content": question})
17
+ response = openai.ChatCompletion.create(
18
+ model="gpt-3.5-turbo-16k-0613",
19
+ messages=self.messages,
20
+ temperature=0,
21
+ max_tokens=2048,
22
+ top_p=1,
23
+ )
24
+ except Exception as e:
25
+ print(e)
26
+ return e
27
+
28
+ message = response["choices"][0]["message"]["content"]
29
+ self.messages.append({"role": "assistant", "content": message})
30
+
31
+ if len(self.messages) > self.num_of_round*2 + 1:
32
+ del self.messages[1:3]
33
+ return message
34
+
35
+ with open('chatbot_03_prompt.md', 'r') as file:
36
+ prompt = file.read()
37
+
38
+ prompt = '"""\n' + prompt + '\n"""'
39
+
40
+ conv = Conversation(prompt, 5)
41
+
42
+ def respond(message, chat_history):
43
+ bot_message = conv.ask(message)
44
+ chat_history.append((message, bot_message))
45
+ return "", chat_history
46
+
47
+ init_conversation = [(None, "欢迎来到 recon! 请问有什么可以帮助你的吗?")]
48
+
49
+ with gr.Blocks(css="#chatbot{height:700px} .overflow-y-auto{height:900px}") as demo:
50
+ chatbot = gr.Chatbot(elem_id="chatbot", value=init_conversation, height="700px")
51
+ state = gr.State([])
52
+
53
+ with gr.Row():
54
+ msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
55
+
56
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
57
+
58
+ demo.launch()
chatbot_03.ipynb ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "9f74e899-4d57-4778-a2fc-c9484279080c",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import gradio as gr\n",
11
+ "import random\n",
12
+ "import openai\n",
13
+ "import os\n",
14
+ "from dotenv import load_dotenv, find_dotenv\n",
15
+ "\n",
16
+ "_ = load_dotenv(find_dotenv())\n",
17
+ "\n",
18
+ "openai.api_key = os.getenv('OPENAI_API_KEY')"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": 3,
24
+ "id": "8660af3a-7d7c-4f89-a127-6eb6942a4dda",
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "class Conversation:\n",
29
+ " def __init__(self, prompt, num_of_round):\n",
30
+ " self.prompt = prompt\n",
31
+ " self.num_of_round = num_of_round\n",
32
+ " self.messages = []\n",
33
+ " self.messages.append({\"role\": \"system\", \"content\": self.prompt})\n",
34
+ "\n",
35
+ " def ask(self, question):\n",
36
+ " try:\n",
37
+ " self.messages.append( {\"role\": \"user\", \"content\": question})\n",
38
+ " response = openai.ChatCompletion.create(\n",
39
+ " model=\"gpt-3.5-turbo\",\n",
40
+ " messages=self.messages,\n",
41
+ " temperature=0.5,\n",
42
+ " max_tokens=300,\n",
43
+ " top_p=1,\n",
44
+ " )\n",
45
+ " except Exception as e:\n",
46
+ " print(e)\n",
47
+ " return e\n",
48
+ "\n",
49
+ " message = response[\"choices\"][0][\"message\"][\"content\"]\n",
50
+ " self.messages.append({\"role\": \"assistant\", \"content\": message})\n",
51
+ " \n",
52
+ " if len(self.messages) > self.num_of_round*2 + 1:\n",
53
+ " del self.messages[1:3]\n",
54
+ " return message"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": 4,
60
+ "id": "47d1dd3e-b84e-4713-9a20-0b5029d548a5",
61
+ "metadata": {},
62
+ "outputs": [
63
+ {
64
+ "name": "stdout",
65
+ "output_type": "stream",
66
+ "text": [
67
+ "Running on local URL: http://127.0.0.1:7860\n",
68
+ "\n",
69
+ "To create a public link, set `share=True` in `launch()`.\n"
70
+ ]
71
+ },
72
+ {
73
+ "data": {
74
+ "text/html": [
75
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
76
+ ],
77
+ "text/plain": [
78
+ "<IPython.core.display.HTML object>"
79
+ ]
80
+ },
81
+ "metadata": {},
82
+ "output_type": "display_data"
83
+ },
84
+ {
85
+ "data": {
86
+ "text/plain": []
87
+ },
88
+ "execution_count": 4,
89
+ "metadata": {},
90
+ "output_type": "execute_result"
91
+ }
92
+ ],
93
+ "source": [
94
+ "with open('chatbot_03_prompt.md', 'r') as file:\n",
95
+ " prompt = file.read()\n",
96
+ "\n",
97
+ "prompt = '\"\"\"\\n' + prompt + '\\n\"\"\"'\n",
98
+ "\n",
99
+ "conv = Conversation(prompt, 5)\n",
100
+ "\n",
101
+ "def respond(message, chat_history):\n",
102
+ " # bot_message = random.choice([\"How are you?\", \"I love you\", \"I'm very hungry\"])\n",
103
+ " bot_message = conv.ask(message)\n",
104
+ " chat_history.append((message, bot_message))\n",
105
+ " return \"\", chat_history\n",
106
+ "\n",
107
+ " \n",
108
+ "init_conversation = [(None, \"欢迎来到我们的 Chatbot! 请问有什么可以帮助你的吗?\")]\n",
109
+ "\n",
110
+ "with gr.Blocks(css=\"#chatbot{height:900px} .overflow-y-auto{height:1200px}\") as demo:\n",
111
+ " chatbot = gr.Chatbot(elem_id=\"chatbot\", value=init_conversation, height=\"900px\")\n",
112
+ " msg = gr.Textbox()\n",
113
+ " msg.submit(respond, [msg, chatbot], [msg, chatbot])\n",
114
+ "\n",
115
+ "demo.launch()"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "code",
120
+ "execution_count": null,
121
+ "id": "2414bf0a-ae3c-490c-bbc5-d6d0fac36490",
122
+ "metadata": {},
123
+ "outputs": [],
124
+ "source": []
125
+ }
126
+ ],
127
+ "metadata": {
128
+ "kernelspec": {
129
+ "display_name": "Python 3 (ipykernel)",
130
+ "language": "python",
131
+ "name": "python3"
132
+ },
133
+ "language_info": {
134
+ "codemirror_mode": {
135
+ "name": "ipython",
136
+ "version": 3
137
+ },
138
+ "file_extension": ".py",
139
+ "mimetype": "text/x-python",
140
+ "name": "python",
141
+ "nbconvert_exporter": "python",
142
+ "pygments_lexer": "ipython3",
143
+ "version": "3.11.4"
144
+ }
145
+ },
146
+ "nbformat": 4,
147
+ "nbformat_minor": 5
148
+ }
chatbot_03_prompt.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ 你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
2
+ 1. 你的回答必须是中文
3
+ 2. 回答限制在100个字以内
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai