huohguohbo commited on
Commit
453dbcd
·
1 Parent(s): 6baa92e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -47
app.py CHANGED
@@ -1,56 +1,45 @@
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")
51
- chat_button = gr.Interface(fn=chatbot, inputs=[input_text, input_api_key], outputs=output_text, title="OpenAI Chatbot", description="An AI chatbot powered by OpenAI's GPT-3 language model.", examples=[["hello", "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"]], allow_flagging=False, layout="vertical", live=False, theme="huggingface", inputs_inline=True, outputs_inline=True)
52
 
53
- return chat_button
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- chat_button = chat_interface()
56
- chat_button.launch()
 
 
 
 
 
 
1
  import openai
2
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # Define the chat function
5
+ def chat(api_key, message):
6
+ # Set up the OpenAI API request
 
 
 
 
 
 
 
7
  response = openai.Completion.create(
8
  engine="davinci",
9
+ prompt=message,
 
10
  max_tokens=1024,
11
+ n=1,
12
+ stop=None,
13
+ temperature=0.5,
14
+ api_key=api_key,
15
  )
16
+
17
+ # Extract the bot's response from the API request
18
+ bot_response = response.choices[0].text.strip()
19
+
20
+ return bot_response
 
 
21
 
22
+ # Define the Gradio interface
23
+ api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
24
+ message_input = gr.inputs.Textbox(label="Enter your message here")
25
+ output = gr.outputs.Textbox(label="Bot response")
 
26
 
27
+ chat_button = gr.Interface(
28
+ fn=chat,
29
+ inputs=[api_key_input, message_input],
30
+ outputs=output,
31
+ title="OpenAI Chatbot",
32
+ description="Enter your message below to chat with an AI",
33
+ theme="compact",
34
+ layout="vertical",
35
+ allow_flagging=False,
36
+ allow_screenshot=False,
37
+ allow_share=False,
38
+ )
39
 
40
+ # Check if an API key has been provided
41
+ if api_key_input.value is None:
42
+ print("Please enter your OpenAI API key and try again.")
43
+ else:
44
+ # Launch the app
45
+ chat_button.launch()