File size: 1,221 Bytes
9d2ea7d
 
 
07eabf3
9d2ea7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import openai
import gradio as gr

openai.api_key = "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"

def openai_chat(input_prompt):
    
    template = """
    Sentence: India won the 1983 Cricket World Cup which was the 3rd edition of the Cricket World Cup tournament.
    Question: Who won the 1983 Cricket World Cup ______ ? Answer: India
    Sentence: Google was founded on September 4, 1998, by Larry Page and Sergey Brin.
    Question: In which year was Google founded ______? Answer: 1998
    """

    input_prompt = "Sentence: " + input_prompt
    prompt = template + input_prompt

    completion = openai.Completion.create(engine="davinci", 
                                      prompt=prompt, 
                                      max_tokens=64, 
                                      temperature=0.7)

    message = completion.choices[0].text
    output_list = message.split("\n")
    out_index = []
    for idx, sentence in enumerate(output_list):
        if "Question" in sentence:
            out_index.append(idx)
    
    if out_index:
        return output_list[min(out_index)]

gr.Interface(fn = openai_chat,
             inputs = ["text"],
             outputs = ["text"]).launch(debug = True)