Spaces:
Runtime error
Runtime error
Commit
·
8adb407
1
Parent(s):
b7e61fc
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import time
|
|
|
|
|
4 |
|
5 |
# The cache dictionary stores the responses to previous requests
|
6 |
cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def chatbot(prompt, api_key):
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def chat_interface():
|
32 |
-
chat_history = []
|
33 |
-
chat_history.append("Bot: Hi there! How can I assist you today?")
|
34 |
-
|
35 |
input_api_key = gr.inputs.Textbox(label="Enter your OpenAI API key")
|
36 |
input_text = gr.inputs.Textbox(lines=7, label="Enter your message")
|
37 |
output_text = gr.outputs.Textbox(label="Bot's reply")
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import time
|
4 |
+
import numpy as np
|
5 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
6 |
|
7 |
# The cache dictionary stores the responses to previous requests
|
8 |
cache = {}
|
9 |
+
chat_history = []
|
10 |
+
|
11 |
+
def get_similarity(message1, message2):
|
12 |
+
# Convert the messages to arrays of word vectors
|
13 |
+
vectorizer = CountVectorizer().fit_transform([message1, message2])
|
14 |
+
vectors = vectorizer.toarray()
|
15 |
+
# Calculate the cosine similarity between the vectors
|
16 |
+
similarity = np.dot(vectors[0], vectors[1]) / (np.linalg.norm(vectors[0]) * np.linalg.norm(vectors[1]))
|
17 |
+
# Return the similarity score
|
18 |
+
return similarity
|
19 |
|
20 |
def chatbot(prompt, api_key):
|
21 |
+
global cache, chat_history
|
22 |
+
similarity_threshold = 0.8 # Set the similarity threshold for repeated requests
|
23 |
+
# Compare the current message to the previous messages and see if they are similar enough to be considered a repeated request
|
24 |
+
for message in chat_history:
|
25 |
+
similarity = get_similarity(prompt, message)
|
26 |
+
if similarity >= similarity_threshold:
|
27 |
+
return "You already asked me that!"
|
28 |
+
# If the prompt is not a repeated request, proceed with the chatbot logic
|
29 |
+
openai.api_key = api_key
|
30 |
+
response = openai.Completion.create(
|
31 |
+
engine="davinci",
|
32 |
+
prompt=prompt,
|
33 |
+
temperature=0.7,
|
34 |
+
max_tokens=1024,
|
35 |
+
top_p=1,
|
36 |
+
frequency_penalty=0,
|
37 |
+
presence_penalty=0
|
38 |
+
)
|
39 |
+
message = response.choices[0].text.strip()
|
40 |
+
# Add the response to the cache and chat history
|
41 |
+
cache[prompt] = message
|
42 |
+
chat_history.append(prompt)
|
43 |
+
# Wait for a short time to avoid hitting the rate limit of the OpenAI API
|
44 |
+
time.sleep(0.1)
|
45 |
+
return message
|
46 |
|
47 |
def chat_interface():
|
|
|
|
|
|
|
48 |
input_api_key = gr.inputs.Textbox(label="Enter your OpenAI API key")
|
49 |
input_text = gr.inputs.Textbox(lines=7, label="Enter your message")
|
50 |
output_text = gr.outputs.Textbox(label="Bot's reply")
|