from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run before the form opens.
def primary_color_1_click(self, **event_args):
# Access the value from the app
question_text = self.question.text
# Call the jupyter notebook uplink method
result = anvil.server.call('answer_question', question_text)
# Show the result to the user
self.answers.text = result
And I am trying to run a code block in Jupyter:
# Tells the jupyter server that this is a an Anvil callable function
@anvil.server.callable
# Define the function that is going to do our NLP
def answer_question(user_question):
# Encode the user question and dataset questions
question_embedding = model.encode([user_question])
dataset_embeddings = model.encode(df['questions'].tolist())
# Compute cosine similarity between the user question and dataset questions
similarities = cosine_similarity(question_embedding, dataset_embeddings)[0]
# Find the most similar questions
top_indices = similarities.argsort()[-6:-1][::-1] # Get the top 5 similar questions
top_similar_questions = df.iloc[top_indices]['questions'].tolist()
top_similar_values = similarities[top_indices]
# Find the most similar question
most_similar_index = similarities.argmax()
highest_similarity = similarities[most_similar_index]
# If the similarity is above a threshold, return the corresponding answer
threshold = 0.7 # Adjust the threshold as per your preference
if highest_similarity > threshold:
answer = df.iloc[most_similar_index]['answers']
return answer
else:
return "Sorry, I couldn't find an answer to your question."
Solved the error. My client side as well as server side both were kept separate. However I had downloaded the anvil code and stored it in my local directory and had named it anvil.py. This anvil.py was getting imported instead of the actual anvil library.
After resolving the naming conflict, I was able to import the anvil.server module in my Jupyter notebook without any issues.