awaisrwp commited on
Commit
94a72a8
·
1 Parent(s): 68efcfb

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +3 -9
  2. app.py +149 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Care Taker
3
- emoji: 🐨
4
- colorFrom: red
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 4.0.2
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: care_taker
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.50.2
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import speech_recognition as sr
3
+ # import pickle
4
+ # import nltk
5
+ # from nltk.corpus import wordnet
6
+ # import pandas as pd
7
+ import difflib
8
+ import gradio as gr
9
+ from transformers import pipeline
10
+ import numpy as np
11
+
12
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
13
+
14
+
15
+ # nltk.download('wordnet')
16
+
17
+
18
+
19
+ class Model_Voice_Text():
20
+
21
+ """
22
+ This class takes the voices, convert them to text
23
+ """
24
+ #open and read the file after the appending:
25
+
26
+ def __init__(self) -> None:
27
+ self.SR_obj = sr.Recognizer()
28
+ self.KEYWORDS = ['suicide', 'urgent', 'poor', 'in-need', 'old', 'pregnant', 'refugee', 'new immigrant', 'patient', 'ill', 'sick', 'anxiety', 'anxious']
29
+ # self.fuzzer = fuzz.Fuzz()
30
+
31
+ # Define a function to find the number of times the word similar to the word stored in variable target_var, in a text stored in a variable named text_res
32
+ def find_similar_word_count(self, text, target_var):
33
+ """Finds the number of times the word similar to the word stored in variable target_var, in a text stored in a variable named text_res using difflib.
34
+
35
+ Args:
36
+ text: The text to search.
37
+ target_var: The word to find the similar word to.
38
+
39
+ Returns:
40
+ The number of times the word similar to target_var appears in the text.
41
+ """
42
+
43
+ # Create a list of all words in the text
44
+ words = text.split()
45
+
46
+ # Find all words similar to target_var
47
+ similar_words = difflib.get_close_matches(target_var, words, cutoff=0.75)
48
+
49
+ # Return the number of similar words
50
+ return len(similar_words)
51
+
52
+
53
+ def matching_text(self, text):
54
+ ret = []
55
+ # words = nltk.word_tokenize(text)
56
+ for target_var in self.KEYWORDS:
57
+ count = self.find_similar_word_count(text, target_var)
58
+
59
+ # matches = process.extract(text, word)
60
+ if count>0:
61
+ ret.append((target_var, count))
62
+ if ret == []:
63
+ ret.append("nothing found")
64
+
65
+ ret.append(text)
66
+ return ret
67
+
68
+ def transcribe(self, audio):
69
+ # sr, y = audio
70
+ # y = y.astype(np.float32)
71
+ # y /= np.max(np.abs(y))
72
+
73
+ return transcriber(audio)["text"]
74
+
75
+ def voice_to_text_s(self, audio):
76
+ # SR_obj = self.SR_obj
77
+ # info = sr.AudioFile(audio)
78
+ tran_text = self.transcribe(audio)
79
+ match_results = self.matching_text(tran_text.lower())
80
+ return match_results
81
+
82
+ # print(info)
83
+
84
+ # with info as source:
85
+ # SR_obj.adjust_for_ambient_noise(source)
86
+ # audio_data = SR_obj.record(source,duration=100)
87
+ # result = SR_obj.recognize_google(audio_data)
88
+ # match_results = self.matching_text(result)
89
+ # return match_results
90
+
91
+
92
+ def voice_to_text(self, voicefolder):
93
+ SR_obj = self.SR_obj
94
+ text_list = []
95
+ res_list = []
96
+
97
+ for subdir, dirs, files in os.walk(voicefolder):
98
+ for file in files:
99
+ print(os.path.join(subdir, file))
100
+ info = sr.AudioFile(os.path.join(subdir, file))
101
+ print(info)
102
+
103
+ with info as source:
104
+ SR_obj.adjust_for_ambient_noise(source)
105
+ audio_data = SR_obj.record(source,duration=100)
106
+ result = SR_obj.recognize_google(audio_data)
107
+ text_list.append(result)
108
+ match_results = self.matching_text(result)
109
+ res_list.append([file, match_results, result])
110
+
111
+ return(text_list, res_list)
112
+
113
+
114
+ model = Model_Voice_Text()
115
+
116
+ # path = "/home/si-lab/Desktop/Projects/DataSciencePrpjects/Voice_records"
117
+ # text, results = model.voice_to_text(path)
118
+
119
+ # f = open("demofile2.txt", "a")
120
+ # f.write(text)
121
+ # f.close()
122
+ # df = pd.DataFrame(results)
123
+ # df.to_csv("list.csv", index=False)
124
+
125
+ demo = gr.Blocks()
126
+
127
+
128
+ micro_ph = gr.Interface(fn=model.voice_to_text_s,
129
+ inputs=gr.Audio(source="microphone", type="filepath"),
130
+ outputs=gr.Textbox(label="Output Box"))
131
+
132
+ file_ph = gr.Interface(fn=model.voice_to_text_s,
133
+ inputs=gr.Audio(source="upload", type="filepath"),
134
+ outputs=gr.Textbox(label="Output Box"))
135
+
136
+
137
+ with demo:
138
+ gr.TabbedInterface(
139
+ [micro_ph, file_ph],
140
+ ["Transcribe Microphone", "Transcribe Audio File"],
141
+ )
142
+
143
+ demo.launch(debug=True)
144
+
145
+ # pickle.dump(model, open("voice_txt.pkl", "wb"))
146
+
147
+
148
+
149
+