mmccanse commited on
Commit
b8b4d41
·
1 Parent(s): 9d45390

convert Jupyter notebook to .py file

Browse files
Files changed (1) hide show
  1. merge_NEW_mm.py +126 -0
merge_NEW_mm.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[1]:
5
+
6
+
7
+ # pip install langdetect
8
+ # pip install sentencepiece
9
+ # pip install boto3
10
+ # pip install awscli
11
+ # pip install sacremoses
12
+
13
+
14
+ # In[2]:
15
+
16
+
17
+ import gradio as gr
18
+ from transformers import pipeline, AutoTokenizer, TFAutoModelForSeq2SeqLM
19
+ from dotenv import load_dotenv
20
+ import os
21
+ import subprocess
22
+ import torch
23
+ import tempfile
24
+ from langdetect import detect
25
+ from transformers import MarianMTModel, MarianTokenizer
26
+ import re
27
+ import boto3
28
+
29
+
30
+ # In[3]:
31
+
32
+
33
+ # import functions from functions file
34
+
35
+ from functions_mm import handle_query, transcribe_audio_original, submit_question, polly_text_to_speech, translate, translate_and_speech, clear_inputs, voice_map, language_map, default_language, languages
36
+
37
+
38
+
39
+ # In[4]:
40
+
41
+
42
+ # Load environment variables.
43
+ load_dotenv()
44
+
45
+ # Set the model name for our LLMs.
46
+ OPENAI_MODEL = "gpt-3.5-turbo"
47
+ # Store the API key in a variable.
48
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
49
+
50
+
51
+ # In[5]:
52
+
53
+
54
+ instructions = """
55
+ # Diabetes Chatbot
56
+ #### Step 1: Record your audio OR input text (NOT both!)
57
+ #### Step 2: Would you like response in new language? Choose your language
58
+ #### Step 3: Submit question
59
+ #### Step 4: Translate response
60
+ #### Step 5: Clear inputs and start fresh
61
+ """
62
+
63
+
64
+ with gr.Blocks() as app2:
65
+
66
+ with gr.Row():
67
+ gr.Markdown(instructions)
68
+
69
+ with gr.Row():
70
+ input_audio = gr.Audio(
71
+ label="Ask a question about Diabetes, then click 'Transcribe audio",
72
+ type="filepath")
73
+ language_dropdown = gr.Dropdown(label="Click the middle of the dropdown bar to select translation language",
74
+ choices=list(language_map.keys()), value=default_language, type='value')
75
+
76
+ with gr.Row():
77
+ transcribe_button = gr.Button("Transcribe audio")
78
+ submit_button = gr.Button("Submit your question")
79
+ translate_button = gr.Button("Translate the response")
80
+ clear_button = gr.Button("Clear All")
81
+
82
+ #Divide the screen horizontally into 2 columns
83
+ with gr.Row():
84
+ #This column will be on the left side of screen
85
+ with gr.Column():
86
+ query_text = gr.Textbox(label="Type your question here. If there is audio recorded AND question text, app will submit question text. Click transcribe button to populate with audio text")
87
+ # output_original_speech = gr.Audio(label="Text to speech here")
88
+
89
+ with gr.Column():
90
+ response_text = gr.Textbox(label="Chatbot response")
91
+ response_speech = gr.Audio(label="Chatbot response speech")
92
+
93
+ #This column will be on the right side of screen
94
+ with gr.Column():
95
+ output_translated = gr.Textbox(label="Translated text")
96
+ output_translated_speech = gr.Audio(label="Translated speech")
97
+
98
+ # Audio transcription
99
+ transcribe_button.click(
100
+ fn=transcribe_audio_original,
101
+ inputs=[input_audio],
102
+ outputs=[query_text]
103
+ )
104
+
105
+ submit_button.click(
106
+ fn=submit_question,
107
+ inputs=[input_audio, query_text, language_dropdown],
108
+ outputs=[response_text, response_speech]
109
+ )
110
+
111
+ # Translation
112
+ translate_button.click(
113
+ fn=translate_and_speech,
114
+ inputs=[response_text, language_dropdown],
115
+ outputs=[output_translated, output_translated_speech]
116
+ )
117
+
118
+ #Clearing all inputs and outputs
119
+ clear_button.click(
120
+ fn=clear_inputs,
121
+ inputs=[],
122
+ outputs=[input_audio, query_text, response_text, response_speech, output_translated, output_translated_speech]
123
+ )
124
+
125
+ app2.launch(show_error=True, share=True)
126
+