Jagukumar/Text-To-Embeddings
3
1from processing import extract_text, preprocess_text_generalized, get_embeddings_from_huggingface2import gradio as gr3import numpy as np4import spacy5import os6 7# Check if SpaCy model is downloaded; if not, download it8try:9 nlp = spacy.load("en_core_web_sm")10except OSError:11 os.system("python -m spacy download en_core_web_sm")12 nlp = spacy.load("en_core_web_sm")13 14 15def process_file(file_path):16 try:17 # Step 1: Extract text18 extracted_text = extract_text(file_path)19 20 # Step 2: Preprocess text21 cleaned_text = preprocess_text_generalized(extracted_text)22 23 # Step 3: Generate embeddings24 embeddings = get_embeddings_from_huggingface(cleaned_text)25 26 # Step 4: Save embeddings to a temporary file27 temp_file_path = "embeddings.npy"28 np.save(temp_file_path, embeddings)29 30 # Return the top 10 embeddings and the file path for download31 top_10_embeddings = embeddings[:10].tolist()32 return f"Top 10 Embeddings: {top_10_embeddings}", temp_file_path33 except Exception as e:34 return str(e), None35 36# Define Gradio Interface37interface = gr.Interface(38 fn=process_file,39 inputs=gr.File(label="Upload a file (CSV, PDF, JSON)", type="filepath"),40 outputs=[41 gr.Textbox(label="Top 10 Embeddings"),42 gr.File(label="Download Full Embeddings"),43 ],44 title="Embedding Converter Using Hugging Face Model",45 description=(46 "Upload a file (CSV, PDF, or JSON) to generate embeddings using "47 "Hugging Face models. View the top 10 embeddings and download entire embedding file."48 ),49)50 51if __name__ == "__main__":52 interface.launch()53 