huohguohbo commited on
Commit
8adb407
·
1 Parent(s): b7e61fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -24
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
- if prompt in cache:
10
- # If the prompt is already in the cache, return the cached response
11
- return cache[prompt]
12
- else:
13
- # If the prompt is not in the cache, make a request to the OpenAI API
14
- openai.api_key = api_key
15
- response = openai.Completion.create(
16
- engine="davinci",
17
- prompt=prompt,
18
- temperature=0.7,
19
- max_tokens=1024,
20
- top_p=1,
21
- frequency_penalty=0,
22
- presence_penalty=0
23
- )
24
- message = response.choices[0].text.strip()
25
- # Add the response to the cache
26
- cache[prompt] = message
27
- # Wait for a short time to avoid hitting the rate limit of the OpenAI API
28
- time.sleep(0.1)
29
- return message
 
 
 
 
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")