Commit 2b0781aa authored by ParamiLelkada's avatar ParamiLelkada

new mongo creds

parent 823c9ce0
......@@ -3,4 +3,5 @@ numpy==1.24.2
transformers
torch
pymongo
requests
\ No newline at end of file
requests
gtts
import random
import pymongo
client = pymongo.MongoClient("mongodb+srv://hearme:hearme678@cluster0.kz66vdr.mongodb.net")
client = pymongo.MongoClient("mongodb+srv://user123:hhCsLKxVQoWDsO2M@hearme.5yquvxf.mongodb.net/")
db = client['word_card']
collection = db['card']
......
......@@ -2,11 +2,29 @@ import torch
from transformers import RobertaTokenizer, RobertaForMaskedLM
import pymongo
from content_filter_azure import is_word_safe
from gtts import gTTS
import os
# Load the pretrained RoBERTa model and tokenizer
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaForMaskedLM.from_pretrained('roberta-base')
def generate_audio_save_locally(word):
# Define the path where you want to save the audio file
# Using a temporary directory or a specific path
temp_dir = "./temp_audio"
os.makedirs(temp_dir, exist_ok=True) # Ensure the directory exists
# Define the full path for the audio file
file_path = os.path.join(temp_dir, f"{word}.mp3")
# Generate the audio using gTTS
tts = gTTS(text=word, lang='en')
tts.save(file_path)
# Return the local path of the generated audio file
return file_path
def get_similar_words(input_word, top_k=3):
# Create a masked sentence with the input word
masked_sentence = f"The {input_word} is related to the {tokenizer.mask_token}."
......@@ -35,7 +53,7 @@ def get_similar_words(input_word, top_k=3):
result = []
for word in related_words:
image_url = f'https://fyp-word-images.s3.us-east-2.amazonaws.com/{word}.png'
audio_url = f'https://fyp-word-audio.s3.us-east-2.amazonaws.com/{word}.m4a'
audio_url = generate_audio_save_locally(word)
result.append({
'word': word,
'image': image_url,
......@@ -44,7 +62,7 @@ def get_similar_words(input_word, top_k=3):
#connect mongo
client = pymongo.MongoClient("mongodb+srv://hearme:hearme678@cluster0.kz66vdr.mongodb.net")
client = pymongo.MongoClient("mongodb+srv://user123:hhCsLKxVQoWDsO2M@hearme.5yquvxf.mongodb.net/")
db = client['word_card']
collection = db['card']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment