RamV commited on
Commit
36b41f7
·
1 Parent(s): 5376ec3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Check if user input contains the phrase "who created you"
16
+ if "who created you" in user_input.lower():
17
+ # Generate a response that includes your name
18
+ chat_response = f"I was created by [YOUR NAME HERE]!"
19
+ else:
20
+ # Construct the prompt with the user input
21
+ prompt = f"You said: {user_input}"
22
+
23
+ # Generate a response using the OpenAI API
24
+ response = openai.Completion.create(
25
+ engine="text-davinci-002",
26
+ prompt=prompt,
27
+ temperature=0.7,
28
+ max_tokens=1024,
29
+ n=1,
30
+ stop=None,
31
+ frequency_penalty=0,
32
+ presence_penalty=0
33
+ )
34
+
35
+ # Extract the response from the API output
36
+ chat_response = response.choices[0].text.strip()
37
+
38
+ return chat_response
39
+
40
+ # Define the function to recognize speech and get a response from the chatbot
41
+ def recognize_speech():
42
+ r = sr.Recognizer()
43
+ with sr.Microphone() as source:
44
+ print("Speak:")
45
+ audio = r.listen(source)
46
+
47
+ try:
48
+ user_input = r.recognize_google(audio)
49
+ print("You said: " + user_input)
50
+ except sr.UnknownValueError:
51
+ user_input = ""
52
+ print("Could not understand audio")
53
+ except sr.RequestError as e:
54
+ user_input = ""
55
+ print("Could not request results; {0}".format(e))
56
+
57
+ chat_response = generate_response(user_input)
58
+
59
+ return chat_response
60
+
61
+ # Create a Gradio interface for the chatbot with speech recognition
62
+ interface = gr.Interface(fn=recognize_speech,
63
+ inputs=None,
64
+ outputs=gr.outputs.Textbox(label="ChatRobo Output"))
65
+ interface.launch()