wdsawdsawdadad huohguohbo commited on
Commit
3575e16
0 Parent(s):

Duplicate from huohguohbo/Chatbot_REQUIRES_OPENAI_KEY

Browse files

Co-authored-by: wyattb <huohguohbo@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +105 -0
  4. requirements.txt +3 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chatbot REQUIRES OPENAI KEY
3
+ emoji: 👁
4
+ colorFrom: red
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.27.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: huohguohbo/Chatbot_REQUIRES_OPENAI_KEY
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import transformers
3
+ import gradio as gr
4
+
5
+ # Set up the OpenAI API client
6
+ openai.api_key = "YOUR_API_KEY"
7
+
8
+ # Define the chat function for OpenAI API
9
+ def openai_chat(api_key, model, message):
10
+ # Check if an API key has been provided
11
+ if api_key is None:
12
+ return "Please enter your OpenAI API key and try again."
13
+
14
+ # Set up the OpenAI API request
15
+ response = openai.Completion.create(
16
+ engine=model,
17
+ prompt=message,
18
+ max_tokens=1024,
19
+ n=1,
20
+ stop=None,
21
+ temperature=0.5,
22
+ api_key=api_key,
23
+ )
24
+
25
+ # Extract the bot's response from the API request
26
+ bot_response = response.choices[0].text.strip()
27
+
28
+ return bot_response
29
+
30
+ # Define the chat function for Hugging Face API
31
+ def hf_chat(model_name, message):
32
+ # Load the model and tokenizer
33
+ model = transformers.pipeline("text-generation", model=model_name)
34
+
35
+ # Generate a response from the model
36
+ bot_response = model(message, max_length=1024, do_sample=True, temperature=0.7)[0]["generated_text"]
37
+
38
+ return bot_response
39
+
40
+ # Define the Gradio interface for chatbot
41
+ api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None, block="sidebar")
42
+ model_input = gr.inputs.Dropdown(
43
+ label="Select OpenAI model",
44
+ choices=["davinci", "curie", "babbage"],
45
+ default="davinci",
46
+ block="sidebar"
47
+ )
48
+ hf_model_input = gr.inputs.Dropdown(
49
+ label="Select Hugging Face model",
50
+ choices=["microsoft/DialoGPT-large", "Salesforce/codegen-2B-multi", "microsoft/DialoGPT-small"],
51
+ default="microsoft/DialoGPT-large",
52
+ block="sidebar"
53
+ )
54
+ mode_input = gr.inputs.Dropdown(
55
+ label="Select chatbot mode",
56
+ choices=["OpenAI", "Hugging Face"],
57
+ default="OpenAI",
58
+ block="sidebar"
59
+ )
60
+ message_input = gr.inputs.Textbox(label="Enter your message here", block="input")
61
+ output = gr.outputs.Textbox(label="Bot response", block="output")
62
+
63
+ # Define the chat window
64
+ chat_window = []
65
+
66
+ def chatbot(chat_window, message, mode, model, hf_model, api_key, send_button, clear_button):
67
+ if clear_button:
68
+ chat_window.clear()
69
+ return "Chat history cleared."
70
+ if send_button:
71
+ if message:
72
+ if mode == "Hugging Face":
73
+ bot_response = hf_chat(hf_model, message)
74
+ else:
75
+ bot_response = openai_chat(api_key, model, message)
76
+ chat_window.append(("User", message))
77
+ chat_window.append(("Bot", bot_response))
78
+ return "\n".join([f"{name}: {text}" for name, text in chat_window])
79
+
80
+ # Define the Gradio interface for chatbot
81
+ send_button = gr.inputs.Button(label="Send")
82
+ clear_button = gr.inputs.Button(label="Clear Chat History")
83
+ chat_interface = gr.Interface(
84
+ fn=chatbot,
85
+ inputs=[
86
+ message_input,
87
+ mode_input,
88
+ model_input,
89
+ hf_model_input,
90
+ api_key_input,
91
+ send_button,
92
+ clear_button
93
+ ],
94
+ outputs=output,
95
+ title="Chatbot",
96
+ description="Enter your message below to chat with an AI",
97
+ theme="compact",
98
+ allow_flagging=False,
99
+ allow_screenshot=False,
100
+ allow_share=False,
101
+ layout="vertical"
102
+ )
103
+
104
+ # Launch the page
105
+ chat_interface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ transformers
3
+ torch