Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""code-search.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1-TlihNx5XCiVSxUHDF1oHFNcfpuy_k0N
|
8 |
+
"""
|
9 |
+
|
10 |
+
# Install Cohere for embeddings
|
11 |
+
|
12 |
+
import cohere
|
13 |
+
import numpy as np
|
14 |
+
import pandas as pd
|
15 |
+
import gradio as gr
|
16 |
+
|
17 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
18 |
+
from annoy import AnnoyIndex
|
19 |
+
import warnings
|
20 |
+
warnings.filterwarnings('ignore')
|
21 |
+
pd.set_option('display.max_colwidth', None)
|
22 |
+
|
23 |
+
data_df = pd.read_csv('functions_data.csv')
|
24 |
+
#data_df.head()
|
25 |
+
|
26 |
+
data_df['docstring'].fillna('not specified', inplace=True)
|
27 |
+
|
28 |
+
# Paste your API key here. Remember to not share publicly
|
29 |
+
api_key = '2IdvZuDAwqcpMuwN3yjAXBOHKAT1Mqxr4N8hZFKN'
|
30 |
+
|
31 |
+
# Create and retrieve a Cohere API key from dashboard.cohere.ai/welcome/register
|
32 |
+
co = cohere.Client(api_key)
|
33 |
+
|
34 |
+
|
35 |
+
search_index = AnnoyIndex(4096, 'angular')
|
36 |
+
search_index.load('code.ann') # super fast, will just mmap the file
|
37 |
+
|
38 |
+
def get_code(query):
|
39 |
+
# Get the query's embedding
|
40 |
+
query_embed = co.embed(texts=[query],
|
41 |
+
model="large",
|
42 |
+
truncate="LEFT").embeddings
|
43 |
+
|
44 |
+
# Retrieve the nearest neighbors
|
45 |
+
similar_item_ids = search_index.get_nns_by_vector(query_embed[0],3,
|
46 |
+
include_distances=True)
|
47 |
+
|
48 |
+
return data_df.iloc[similar_item_ids[0]]['function_body'] , data_df.iloc[similar_item_ids[0]]['file_path']
|
49 |
+
|
50 |
+
iface = gr.Interface(fn=get_code, inputs="text", outputs=[gr.Markdown(), "text"])
|
51 |
+
iface.launch()
|
52 |
+
|
53 |
+
|
54 |
+
|