Spaces:
Runtime error
Runtime error
hrishikesh
commited on
Commit
·
9d2ea7d
1
Parent(s):
cd2910e
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
openai.api_key = "NTZ1vJAJ6xFNk8ibiQjVh8E5CQLafLpKxjWJc5jp"
|
| 5 |
+
|
| 6 |
+
def openai_chat(input_prompt):
|
| 7 |
+
|
| 8 |
+
template = """
|
| 9 |
+
Sentence: India won the 1983 Cricket World Cup which was the 3rd edition of the Cricket World Cup tournament.
|
| 10 |
+
Question: Who won the 1983 Cricket World Cup ______ ? Answer: India
|
| 11 |
+
Sentence: Google was founded on September 4, 1998, by Larry Page and Sergey Brin.
|
| 12 |
+
Question: In which year was Google founded ______? Answer: 1998
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
input_prompt = "Sentence: " + input_prompt
|
| 16 |
+
prompt = template + input_prompt
|
| 17 |
+
|
| 18 |
+
completion = openai.Completion.create(engine="davinci",
|
| 19 |
+
prompt=prompt,
|
| 20 |
+
max_tokens=64,
|
| 21 |
+
temperature=0.7)
|
| 22 |
+
|
| 23 |
+
message = completion.choices[0].text
|
| 24 |
+
output_list = message.split("\n")
|
| 25 |
+
out_index = []
|
| 26 |
+
for idx, sentence in enumerate(output_list):
|
| 27 |
+
if "Question" in sentence:
|
| 28 |
+
out_index.append(idx)
|
| 29 |
+
|
| 30 |
+
if out_index:
|
| 31 |
+
return output_list[min(out_index)]
|
| 32 |
+
|
| 33 |
+
gr.Interface(fn = openai_chat,
|
| 34 |
+
inputs = ["text"],
|
| 35 |
+
outputs = ["text"]).launch(debug = True)
|