RamV commited on
Commit
b984ecb
·
1 Parent(s): 9179c2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -37
app.py CHANGED
@@ -1,50 +1,61 @@
1
  import os
2
  import openai
3
  import gradio as gr
4
- import speech_recognition as sr
5
 
6
  # Set up OpenAI API credentials
7
  api_key = os.environ.get("OPENAI_API_KEY")
8
  openai.api_key = api_key
9
 
10
- # Define the conversation prompt
11
- conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
12
-
13
- # Define the function to generate a response from the GPT model
14
  def generate_response(user_input):
15
- try:
16
- # Check if user input contains the phrase "who created you"
17
- if "who created you" in user_input.lower():
18
- # Generate a response that includes your name
19
- chat_response = f"I was created by [Ramv]!"
20
- else:
21
- # Construct the prompt with the user input
22
- prompt = f"You said: {user_input}"
23
-
24
- # Generate a response using the OpenAI API
25
- response = openai.Completion.create(
26
- engine="text-davinci-002",
27
- prompt=prompt,
28
- temperature=0.7,
29
- max_tokens=1024,
30
- n=1,
31
- stop=None,
32
- frequency_penalty=0,
33
- presence_penalty=0
34
- )
35
-
36
- # Extract the response from the API output
37
- chat_response = response.choices[0].text.strip()
38
-
39
- except Exception as e:
40
- # If an error occurs during the API request, return an error message
41
- chat_response = "Sorry, an error occurred while processing your request. Please try again later."
42
 
43
  return chat_response
44
 
45
- # Create a Gradio interface for the chatbot with speech recognition
46
- interface = gr.Interface(fn=recognize_speech,
47
- inputs=None,
48
- outputs=gr.outputs.Textbox(label="ChatRobo Output"))
49
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
 
1
  import os
2
  import openai
3
  import gradio as gr
 
4
 
5
  # Set up OpenAI API credentials
6
  api_key = os.environ.get("OPENAI_API_KEY")
7
  openai.api_key = api_key
8
 
9
+ # Define a function to generate a response to user input
 
 
 
10
  def generate_response(user_input):
11
+ # Check if user input contains the phrase "who created you"
12
+ if "who created you" in user_input.lower():
13
+ # Generate a response that includes your name
14
+ chat_response = "I was created by RamV!"
15
+ else:
16
+ # Construct the prompt with the user input
17
+ prompt = f"You said: {user_input}"
18
+
19
+ # Generate a response using the OpenAI API
20
+ response = openai.Completion.create(
21
+ model="text-davinci-003",
22
+ prompt=prompt,
23
+ temperature=0.9,
24
+ max_tokens=150,
25
+ top_p=1,
26
+ frequency_penalty=0,
27
+ presence_penalty=0.6,
28
+ stop=["Human:", "AI:"]
29
+ )
30
+
31
+ # Extract the response from the API output
32
+ chat_response = response.choices[0].text.strip()
 
 
 
 
 
33
 
34
  return chat_response
35
 
36
+ # Define the function to handle the chat history
37
+ def openai_chat_history(input, history):
38
+ history = history or []
39
+ if input.strip() != "":
40
+ s = list(sum(history, ()))
41
+ s.append(input)
42
+ inp = ' '.join(s)
43
+ output = generate_response(inp)
44
+ history.append((input, output))
45
+ return history[-1][1]
46
+ else:
47
+ return ""
48
+
49
+ # Define the conversation prompt
50
+ conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
51
+
52
+ # Set up the Gradio interface
53
+ block = gr.Interface(
54
+ fn=openai_chat_history,
55
+ inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
56
+ outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
57
+ )
58
+
59
+ # Launch the Gradio interface
60
+ block.launch()
61