-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
192 lines (145 loc) · 5.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from typing import Optional
from entities.model import *
from fastapi import FastAPI
from datetime import datetime
import subprocess, sys, json, io, pathlib, os, proto
from fastapi.middleware.cors import CORSMiddleware
# from summarize import Summarizer
from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
import numpy as np
import networkx as nx
from datetime import datetime
# Imports the Google Cloud client library
from google.cloud import speech
d = datetime.now()
app = FastAPI(
title="Scriptrad",
description="Translation/traduction API",
version="1.0"
)
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
"http://localhost:4200",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/sendAudioByGet/{file_name}")
def read_item(file_name: str):
return file_name
#end tests
@app.post("/transcript")
def transcript(transcript: Transcript):
# Json file about API Key and credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.path.dirname(__file__), "resources", "api_key.json")
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
absolute_current_path = pathlib.Path().absolute()
base = os.path.splitext(transcript.file)[0]
folder_name = "uploads"
filename = transcript.file
flac_filename = base+".flac"
absolute_path = os.path.join(absolute_current_path, folder_name, filename)
absolute_folder_path = os.path.join(absolute_current_path, folder_name)
absolute_folder_flac = os.path.join(absolute_folder_path,flac_filename)
subprocess.check_output(['sox', absolute_path, absolute_folder_flac])
# Loads the audio into memory
with io.open(absolute_folder_flac, "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
audio_channel_count=2,
enable_automatic_punctuation=True,
language_code="fr-FR",
)
# Detects speech in the audio file
response = client.recognize(config=config, audio=audio)
transcription = ""
for result in response.results:
transcription = transcription + result.alternatives[0].transcript
response = {
'message': transcription.replace('\n', '')
}
#print(response.results[0])
#sys.exit()
return json.dumps(response)
@app.post("/traduce")
def traduce(traduce: Traduce):
print("la")
#summarize
import re
def read_article(text):
# file = open(file_name, "r")
# filedata = file.readlines()
#article = filedata[0].split(". ")
sentences = []
article = re.split("\. |, ",text)
for sentence in article:
print(sentence)
sentences.append(sentence.replace("[^a-zA-Z]", " ").split(" "))
sentences.pop()
return sentences
def sentence_similarity(sent1, sent2, stopwords=None):
if stopwords is None:
stopwords = []
sent1 = [w.lower() for w in sent1]
sent2 = [w.lower() for w in sent2]
all_words = list(set(sent1 + sent2))
vector1 = [0] * len(all_words)
vector2 = [0] * len(all_words)
# build the vector for the first sentence
for w in sent1:
if w in stopwords:
continue
vector1[all_words.index(w)] += 1
# build the vector for the second sentence
for w in sent2:
if w in stopwords:
continue
vector2[all_words.index(w)] += 1
return 1 - cosine_distance(vector1, vector2)
def build_similarity_matrix(sentences, stop_words):
# Create an empty similarity matrix
similarity_matrix = np.zeros((len(sentences), len(sentences)))
for idx1 in range(len(sentences)):
for idx2 in range(len(sentences)):
if idx1 == idx2: # ignore if both are same sentences
continue
similarity_matrix[idx1][idx2] = sentence_similarity(sentences[idx1], sentences[idx2], stop_words)
return similarity_matrix
#top_n number of paragraphs without blank line
def generate_summary(textToSummarize, top_n=1):
stop_words = stopwords.words('french')
summarize_text = []
# Step 1 - Read text anc split it
sentences = read_article(textToSummarize)
# Step 2 - Generate Similary Martix across sentences
sentence_similarity_martix = build_similarity_matrix(sentences, stop_words)
# Step 3 - Rank sentences in similarity martix
sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix)
scores = nx.pagerank(sentence_similarity_graph)
# Step 4 - Sort the rank and pick top sentences
ranked_sentence = sorted(((scores[i], s) for i, s in enumerate(sentences)), reverse=True)
print("Indexes of top ranked_sentence order are ", ranked_sentence)
for i in range(top_n):
summarize_text.append(" ".join(ranked_sentence[i][1]))
# Step 5 - Offcourse, output the summarize texr
print("Summarize Text: \n", ". ".join(summarize_text))
#Step 6 return
#return ("Summarize Text: \n", ". ".join(summarize_text))
return summarize_text
@app.post("/summarize")
def resume(summarize: Summarize):
#s = Summarizer()
print("la")
return generate_summary(summarize.text)