Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,47 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
4 |
|
5 |
+
def find_closest(query):
|
6 |
+
files_contents = []
|
7 |
+
files_names = []
|
8 |
|
9 |
+
for file in os.listdir():
|
10 |
+
if file.endswith(".txt"):
|
11 |
+
with open(file, 'r') as f:
|
12 |
+
content = f.read()
|
13 |
+
files_contents.append(content)
|
14 |
+
files_names.append(file)
|
15 |
+
|
16 |
+
# Append query to the end
|
17 |
+
files_contents.append(query)
|
18 |
+
|
19 |
+
# Initialize the TfidfVectorizer
|
20 |
+
tfidf_vectorizer = TfidfVectorizer()
|
21 |
+
|
22 |
+
# Fit and transform the texts
|
23 |
+
tfidf_matrix = tfidf_vectorizer.fit_transform(files_contents)
|
24 |
+
|
25 |
+
# Compute the cosine similarity between the query and all files
|
26 |
+
similarity_scores = cosine_similarity(tfidf_matrix[-1:], tfidf_matrix[:-1])
|
27 |
+
|
28 |
+
# Get the index of the file with the highest similarity score
|
29 |
+
max_similarity_idx = similarity_scores.argmax()
|
30 |
+
|
31 |
+
# Return the name of the file with the highest similarity score
|
32 |
+
return files_names[max_similarity_idx]
|
33 |
+
|
34 |
+
def find_closest_mp3(query):
|
35 |
+
closest_txt_file = find_closest(query)
|
36 |
+
file_name_without_extension, _ = os.path.splitext(closest_txt_file)
|
37 |
+
return file_name_without_extension + '.mp3'
|
38 |
+
my_theme = gr.Theme.from_hub("ysharma/llamas")
|
39 |
+
with gr.Blocks(theme=my_theme) as demo:
|
40 |
+
gr.Markdown("# BeatLlama Dreambooth!")
|
41 |
+
# video=gr.PlayableVideo("final_video.mp4")
|
42 |
+
inp=gr.Textbox(placeholder="Describe your dream!",label="Your dream")
|
43 |
+
out=gr.Audio(label="Llamas singing your dream")
|
44 |
+
inp.change(find_closest_mp3,inp,out,scroll_to_output=True)
|
45 |
+
out.play(None)
|
46 |
+
demo.queue(1)
|
47 |
+
demo.launch(show_api=False,debug=True)
|