SantiagoTesla commited on
Commit
abf1e0b
·
1 Parent(s): 2a6dca7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -10
app.py CHANGED
@@ -1,12 +1,54 @@
1
  import openai
2
  import gradio as gr
 
3
  import warnings
 
 
 
 
 
4
 
5
  warnings.filterwarnings("ignore")
 
6
  openai.api_key = "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"
7
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def transcribe(audio, text):
 
 
10
  if audio is not None:
11
  with open(audio, "rb") as transcript:
12
  prompt = openai.Audio.transcribe("whisper-1", transcript)
@@ -22,18 +64,25 @@ def transcribe(audio, text):
22
  stop=None,
23
  temperature=0.5,
24
  )
25
- r = response.choices[0].text
 
 
 
 
 
 
 
26
 
27
- return [s,r]
28
 
29
 
30
  with gr.Blocks() as demo:
31
- gr.Markdown("Boba Jarvis strikes again")
32
- with gr.Row():
33
- input1 = gr.inputs.Audio(source="microphone", type = "filepath", label="Use your voice to chat")
34
- input2 = gr.inputs.Textbox(lines=7, label="Chat with AI")
35
- output_1 = gr.Textbox(label = "Speech to Text")
36
- output_2 = gr.Textbox(label = "AI Output")
37
  btn = gr.Button("Run")
38
- btn.click(fn=transcribe, inputs=[input1, input2], outputs=[output_1, output_2])
39
- demo.launch()
 
1
  import openai
2
  import gradio as gr
3
+ import time
4
  import warnings
5
+ import warnings
6
+ import os
7
+ from gtts import gTTS
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+ import torch
10
 
11
  warnings.filterwarnings("ignore")
12
+
13
  openai.api_key = "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"
14
 
15
 
16
+ def chatgpt_api(input_text):
17
+ '''messages = [
18
+ {"role": "system", "content": "You are a helpful assistant."}]
19
+
20
+ if input_text:
21
+ messages.append(
22
+ {"role": "user", "content": input_text},
23
+ )
24
+ chat_completion = openai.ChatCompletion.create(
25
+ model="gpt-3.5-turbo", messages=messages
26
+ )
27
+
28
+ reply = chat_completion.choices[0].message.content'''
29
+ input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
30
+ # concatenate new user input with chat history (if there is)
31
+ bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1)
32
+ # generate a bot response
33
+ chat_history_ids = model.generate(
34
+ bot_input_ids,
35
+ max_length=1000,
36
+ do_sample=True,
37
+ top_p=0.95,
38
+ top_k=0,
39
+ temperature=0.75,
40
+ pad_token_id=tokenizer.eos_token_id
41
+ )
42
+ #print the output
43
+ reply = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
44
+
45
+ return reply
46
+
47
+ #ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 10 -q:a 9 -acodec libmp3lame Temp.mp3'
48
+
49
  def transcribe(audio, text):
50
+ language = "en"
51
+
52
  if audio is not None:
53
  with open(audio, "rb") as transcript:
54
  prompt = openai.Audio.transcribe("whisper-1", transcript)
 
64
  stop=None,
65
  temperature=0.5,
66
  )
67
+
68
+ out_result = chatgpt_api(s)
69
+
70
+ audioobj = gTTS(text = out_result,
71
+ lang = language,
72
+ slow = False)
73
+
74
+ audioobj.save("Temp.mp3")
75
 
76
+ return [s, out_result, "Temp.mp3"]
77
 
78
 
79
  with gr.Blocks() as demo:
80
+ gr.Markdown("Dilip can finally talk!?")
81
+ input1 = gr.inputs.Audio(source="microphone", type = "filepath", label="Use your voice to chat")
82
+ input2 = gr.inputs.Textbox(lines=7, label="Chat with AI")
83
+ output_1 = gr.Textbox(label="Text Input")
84
+ output_2 = gr.Textbox(label="Text Output")
85
+ output_3 = gr.Audio("Temp.mp3", label="Speech Output")
86
  btn = gr.Button("Run")
87
+ btn.click(fn=transcribe, inputs=[input1, input2], outputs=[output_1, output_2, output_3])
88
+ demo.launch(share=True)