1

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 :(

kw-limited
  • 11
  • 3
  • additionally I've set the properties of each variable as topic = text, question = text, when = text, num = number, top_k = number in anvil – kw-limited May 19 '22 at 01:01
  • Read this, possibly you should have passed an index (int) not String! https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers#:~:text=The%20function%20will%20try%20to,indices%20must%20be%20integers%20error. – Mohamad Ghaith Alzin May 19 '22 at 01:09

0 Answers0