Aniquel commited on
Commit
ffcf1f4
1 Parent(s): 482bba4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -1,59 +1,67 @@
1
  import openai
2
  import gradio as gr
3
- import csv
 
 
4
  import os
5
 
 
 
 
 
6
  MODEL = "gpt-3.5-turbo"
7
- chat_history_file = "chat_history.csv"
8
 
9
- openai.api_key = os.environ.get("OPENAI_API_KEY")
 
 
 
 
 
 
 
10
 
11
- def generate_chat_response(input_text):
12
- prompt = f"Conversation:\nUser: {input_text}\nAI:"
13
- completion = openai.ChatCompletion.create(
 
 
14
  model=MODEL,
 
15
  max_tokens=3000,
16
  n=1,
17
  stop=None,
18
- temperature=0.5,
19
- messages=[{"role": "user", "content": "Hello!"}]
20
  )
21
- response = (completion.choices[0].message)
22
- return render_template("talkgpt.html", response=response)
23
-
24
-
25
- def chatbot_response(input_text):
26
- # load chat history from file
27
- chat_history = []
28
- with open(chat_history_file, "r") as f:
29
- reader = csv.reader(f)
30
- for row in reader:
31
- chat_history.append({"input": row[0], "output": row[1]})
32
 
33
- # add current input to chat history
34
- chat_history.append({"input": input_text, "output": ""})
35
 
36
- # use OpenAI ChatCompletion model to generate a response
37
- # based on the user's input text
38
- response_text = generate_chat_response(input_text)
 
 
 
39
 
40
- # add response to chat history
41
- chat_history[-1]["output"] = response_text
 
42
 
43
- # save chat history to file
44
- with open(chat_history_file, "w") as f:
45
- writer = csv.writer(f)
46
- for row in chat_history:
47
- writer.writerow([row["input"], row["output"]])
48
 
49
- return response_text
 
50
 
 
51
  chatbot_interface = gr.Interface(
52
  fn=chatbot_response,
53
- inputs=["text", gr.inputs.Voice()],
54
- outputs=["text", gr.outputs.Voice()],
55
  title="Chatbot",
56
  description="Talk to the chatbot using text and speech inputs."
57
  )
58
 
 
59
  chatbot_interface.launch()
 
1
  import openai
2
  import gradio as gr
3
+ from gtts import gTTS
4
+ from io import BytesIO
5
+ from IPython.display import Audio, display
6
  import os
7
 
8
+ # Set OpenAI API key
9
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
10
+
11
+ # Set OpenAI GPT-3 model
12
  MODEL = "gpt-3.5-turbo"
 
13
 
14
+ # Load chat history from file
15
+ with open("chat_history.txt", "r") as f:
16
+ chat_history = f.read().strip()
17
+
18
+ # Define chatbot response function
19
+ def chatbot_response(text_input, voice_input):
20
+ # Concatenate text and voice input
21
+ input_text = text_input + " " + voice_input
22
 
23
+ # Concatenate input text with chat history
24
+ input_text_with_history = input_text + "\n\n" + chat_history
25
+
26
+ # Use OpenAI API to generate response
27
+ response = openai.Completion.create(
28
  model=MODEL,
29
+ prompt=input_text_with_history,
30
  max_tokens=3000,
31
  n=1,
32
  stop=None,
33
+ temperature=0.5
 
34
  )
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Extract response text from OpenAI API output
37
+ response_text = response.choices[0].text.strip()
38
 
39
+ # Convert response text to speech
40
+ tts = gTTS(text=response_text)
41
+ audio_bytes = BytesIO()
42
+ tts.write_to_fp(audio_bytes)
43
+ audio_bytes.seek(0)
44
+ response_audio = Audio(audio_bytes, autoplay=True)
45
 
46
+ # Update chat history with input and response text
47
+ global chat_history
48
+ chat_history += f"\n\nUser: {text_input}\nChatbot: {response_text}"
49
 
50
+ # Save chat history to file
51
+ with open("chat_history.txt", "w") as f:
52
+ f.write(chat_history)
 
 
53
 
54
+ # Return response text and audio for display in interface
55
+ return response_text, response_audio
56
 
57
+ # Define chatbot interface
58
  chatbot_interface = gr.Interface(
59
  fn=chatbot_response,
60
+ inputs=["text", gr.inputs.Microphone(type="live", duration=10)],
61
+ outputs=["text", "audio"],
62
  title="Chatbot",
63
  description="Talk to the chatbot using text and speech inputs."
64
  )
65
 
66
+ # Launch chatbot interface
67
  chatbot_interface.launch()