DINGOLANI commited on
Commit
7167762
·
verified ·
1 Parent(s): bfb7b53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -30,35 +30,52 @@ autocomplete_data = [str(item).strip('"') for item in autocomplete_data] # Clea
30
  # Encode all items in the dataset into embeddings
31
  autocomplete_embeddings = model.encode(autocomplete_data, convert_to_tensor=True)
32
 
33
- # Synonym Expansion Function
34
  def find_synonym(word, top_n=1):
35
- """Finds the most similar word in the dataset based on embeddings."""
36
  query_embedding = model.encode(word, convert_to_tensor=True)
37
  results = util.semantic_search(query_embedding, autocomplete_embeddings, top_k=top_n)
38
  return [autocomplete_data[result['corpus_id']] for result in results[0]]
39
 
40
- # Autocomplete function with synonym handling
 
 
 
 
 
 
41
  def autocomplete(query):
42
  if not query.strip():
43
- return [] # Avoid empty queries
 
 
 
 
44
 
45
- # Find synonyms dynamically
46
- synonyms = find_synonym(query, top_n=3)
47
-
48
  # Perform fuzzy matching with synonyms included
49
- matches = process.extract(query, autocomplete_data + synonyms, scorer=fuzz.partial_ratio, limit=5)
 
 
 
 
 
50
 
51
- # Return list of suggestions
52
- return [match[0] for match in matches]
 
 
 
 
53
 
54
- # Gradio interface
55
  with gr.Blocks() as demo:
56
- gr.Markdown("### AI-Powered Autocomplete with Synonyms")
57
 
58
  query = gr.Textbox(label="Start typing for autocomplete")
59
- autocomplete_output = gr.Textbox(label="Autocomplete Suggestions", lines=5, interactive=False)
 
 
60
 
61
- # Trigger autocomplete on change
62
- query.change(fn=autocomplete, inputs=query, outputs=autocomplete_output)
63
 
64
  demo.launch()
 
30
  # Encode all items in the dataset into embeddings
31
  autocomplete_embeddings = model.encode(autocomplete_data, convert_to_tensor=True)
32
 
33
+ # Function to find synonyms dynamically
34
  def find_synonym(word, top_n=1):
 
35
  query_embedding = model.encode(word, convert_to_tensor=True)
36
  results = util.semantic_search(query_embedding, autocomplete_embeddings, top_k=top_n)
37
  return [autocomplete_data[result['corpus_id']] for result in results[0]]
38
 
39
+ # Function to correct spellings
40
+ def correct_spelling(word):
41
+ matches = process.extract(word, autocomplete_data, scorer=fuzz.ratio, limit=1)
42
+ best_match, score = matches[0]
43
+ return best_match if score > 80 else word # Only correct if confidence > 80%
44
+
45
+ # Autocomplete function with tracking
46
  def autocomplete(query):
47
  if not query.strip():
48
+ return {"Query": query, "Correction": "None", "Synonym": "None", "Suggestions": []}
49
+
50
+ original_query = query.strip()
51
+ corrected_query = correct_spelling(original_query)
52
+ synonym_query = find_synonym(corrected_query, top_n=1)[0] if corrected_query != original_query else corrected_query
53
 
 
 
 
54
  # Perform fuzzy matching with synonyms included
55
+ matches = process.extract(synonym_query, autocomplete_data, scorer=fuzz.partial_ratio, limit=5)
56
+ suggestions = [match[0] for match in matches]
57
+
58
+ # Detect if spelling correction or synonym replacement occurred
59
+ correction_status = f"{original_query} → {corrected_query}" if original_query != corrected_query else "None"
60
+ synonym_status = f"{corrected_query} → {synonym_query}" if corrected_query != synonym_query else "None"
61
 
62
+ return {
63
+ "Query": original_query,
64
+ "Correction": correction_status,
65
+ "Synonym": synonym_status,
66
+ "Suggestions": "\n".join(suggestions)
67
+ }
68
 
69
+ # Gradio UI
70
  with gr.Blocks() as demo:
71
+ gr.Markdown("### AI-Powered Autocomplete with Spell Correction & Synonyms")
72
 
73
  query = gr.Textbox(label="Start typing for autocomplete")
74
+ correction_output = gr.Textbox(label="Spelling Correction Applied", interactive=False)
75
+ synonym_output = gr.Textbox(label="Synonym Applied", interactive=False)
76
+ suggestions_output = gr.Textbox(label="Autocomplete Suggestions", lines=5, interactive=False)
77
 
78
+ # Bind function to UI
79
+ query.change(fn=autocomplete, inputs=query, outputs=[correction_output, synonym_output, suggestions_output])
80
 
81
  demo.launch()