baotoan2002 commited on
Commit
c825c6e
·
1 Parent(s): 5429b11

Upload Chatbot.ipynb

Browse files
Files changed (1) hide show
  1. Chatbot.ipynb +96 -0
Chatbot.ipynb ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import openai\n",
10
+ "import gradio as gr"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 2,
16
+ "metadata": {},
17
+ "outputs": [],
18
+ "source": [
19
+ "openai.api_key = \"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\""
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 3,
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "def openai_chat(prompt):\n",
29
+ " completions = openai.Completion.create(\n",
30
+ " engine=\"text-davinci-003\",\n",
31
+ " prompt=prompt,\n",
32
+ " max_tokens=1024,\n",
33
+ " n=1,\n",
34
+ " temperature=0.5,\n",
35
+ " )\n",
36
+ "\n",
37
+ " message = completions.choices[0].text\n",
38
+ " return message.strip()"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 4,
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "def chatbot(input, history=[]):\n",
48
+ " output = openai_chat(input)\n",
49
+ " history.append((input, output))\n",
50
+ " return history, history"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": null,
56
+ "metadata": {},
57
+ "outputs": [],
58
+ "source": [
59
+ "description = \"Virtual assistant\"\n",
60
+ "title = \"OpenAI Chatbot\"\n",
61
+ "examples = [\n",
62
+ " [\"Hi\", \"Hello!\"],\n",
63
+ "]\n",
64
+ "\n",
65
+ "interface = gr.Interface.load(\"baotoan2002/Chatbot-OpenAI\")"
66
+ ]
67
+ }
68
+ ],
69
+ "metadata": {
70
+ "kernelspec": {
71
+ "display_name": "Python 3",
72
+ "language": "python",
73
+ "name": "python3"
74
+ },
75
+ "language_info": {
76
+ "codemirror_mode": {
77
+ "name": "ipython",
78
+ "version": 3
79
+ },
80
+ "file_extension": ".py",
81
+ "mimetype": "text/x-python",
82
+ "name": "python",
83
+ "nbconvert_exporter": "python",
84
+ "pygments_lexer": "ipython3",
85
+ "version": "3.9.7"
86
+ },
87
+ "orig_nbformat": 4,
88
+ "vscode": {
89
+ "interpreter": {
90
+ "hash": "14958d3aee5f1cad06795f787e54b96185c25fb40dfec723a5be941f3a531b8c"
91
+ }
92
+ }
93
+ },
94
+ "nbformat": 4,
95
+ "nbformat_minor": 2
96
+ }