Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModel
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# ------------------------------
|
| 9 |
+
# Cache model & dataset
|
| 10 |
+
# ------------------------------
|
| 11 |
+
def load_model_and_tokenizer():
|
| 12 |
+
model_ckpt = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 14 |
+
model = AutoModel.from_pretrained(model_ckpt)
|
| 15 |
+
return tokenizer, model
|
| 16 |
+
|
| 17 |
+
def load_embeddings_dataset():
|
| 18 |
+
dataset = load_dataset("ginnigarg/dataset-github-issues-embeddings", split="train")
|
| 19 |
+
dataset.add_faiss_index(column="embeddings")
|
| 20 |
+
return dataset
|
| 21 |
+
|
| 22 |
+
tokenizer, model = load_model_and_tokenizer()
|
| 23 |
+
embeddings_dataset = load_embeddings_dataset()
|
| 24 |
+
|
| 25 |
+
# ------------------------------
|
| 26 |
+
# Embedding helper
|
| 27 |
+
# ------------------------------
|
| 28 |
+
def cls_pooling(model_output):
|
| 29 |
+
return model_output.last_hidden_state[:, 0]
|
| 30 |
+
|
| 31 |
+
def get_embeddings(text_list):
|
| 32 |
+
encoded_input = tokenizer(text_list, padding=True, truncation=True, return_tensors="pt")
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
model_output = model(**encoded_input)
|
| 35 |
+
return cls_pooling(model_output)
|
| 36 |
+
|
| 37 |
+
# ------------------------------
|
| 38 |
+
# Search function for Gradio
|
| 39 |
+
# ------------------------------
|
| 40 |
+
def semantic_search(question):
|
| 41 |
+
question_embedding = get_embeddings([question]).cpu().detach().numpy()
|
| 42 |
+
scores, samples = embeddings_dataset.get_nearest_examples(
|
| 43 |
+
"embeddings", question_embedding, k=5
|
| 44 |
+
)
|
| 45 |
+
samples_df = pd.DataFrame.from_dict(samples)
|
| 46 |
+
samples_df["scores"] = scores
|
| 47 |
+
samples_df.sort_values("scores", ascending=False, inplace=True)
|
| 48 |
+
# Convert to displayable format
|
| 49 |
+
return samples_df[["title", "comments", "html_url", "scores"]]
|
| 50 |
+
|
| 51 |
+
# ------------------------------
|
| 52 |
+
# Gradio Interface
|
| 53 |
+
# ------------------------------
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# GitHub Issues Semantic Search")
|
| 56 |
+
question_input = gr.Textbox(label="Enter your question", value="How can I load a dataset offline?")
|
| 57 |
+
output = gr.Dataframe(headers=["title", "comments", "html_url", "scores"], datatype=["str", "str", "str", "number"])
|
| 58 |
+
search_button = gr.Button("Search")
|
| 59 |
+
|
| 60 |
+
def on_click(question):
|
| 61 |
+
return semantic_search(question)
|
| 62 |
+
|
| 63 |
+
search_button.click(fn=on_click, inputs=[question_input], outputs=[output])
|
| 64 |
+
|
| 65 |
+
# Launch the Gradio app
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|