I call a function to the server from anvil
results = anvil.server.call('ask_qna', topic, question, when='1D', num=10, top_k=3)
the server then returns the results but I get a the following error:
TypeError: string indices must be integers at c:/Users/khl/Documents/ask_qna.py, line 54(query = line) called from Form1, line 24
in the following definition.
Form1, line 24 is the call function from the client side and line54 from ask_qna.py is the query part.
def gather_news(topic, when, num):
gn = GoogleNews()
news = gn.search(topic, when=when)
all_text = []
article_num=0
for i in range(num):
try:
url = news['entries'][i]["links"][0]['href']
article = Article(url)
article.download()
article.parse()
all_text.append(article.text)
article_num+=1
except:
pass
print(f"Number of articles referenced: {article_num}")
all_text = ",".join(all_text)
return all_text
@anvil.server.callable
def ask_qna(topic, question, when='1d', num=10, top_k=3):
print(datetime.now())
print(f"Topic: {topic} | Question: {question}")
qna_set = {
'question': question,
'context': gather_news(topic, when=when, num=num)
}
results = qna(qna_set, tok_k=top_k)
print()
if top_k == 1:
results = [results]
i=0
for item in results:
i+=1
*query = (topic+" "+item['answer']).translate(str.maketrans('','',string.punctuation)).replace(" ", "+")
item.update( {"numbering":i, "url": f"https://news.google.com/search?for=q={query}+when:{when}&hl=en"})*
for result in results:
print(f"Rank: {result['numbering']}\nAnswer: {result['answer']}\nLikelihood: {round(result['score'],4)}\n")
return results
any help would be much appreciated :(