File size: 2,563 Bytes
2ca06ea
 
 
 
 
 
 
7cec07d
3e1cc4e
234ed52
7cec07d
2ca06ea
7cec07d
 
3e1cc4e
7cec07d
 
9a635c6
5455d50
7cec07d
 
 
2ca06ea
a8b1af3
7cec07d
 
 
9a635c6
7cec07d
2ca06ea
 
 
 
 
 
7cec07d
2ca06ea
7cec07d
2ca06ea
 
7cec07d
 
2ca06ea
 
7cec07d
 
 
3e1cc4e
2ca06ea
1326023
2ca06ea
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import os
import openai

openai.organization = os.environ.get('ORGANIZATION')
openai.api_key = os.environ.get('API_KEY')

def classificate_with_gpt3(text, labels, engine="text-similarity-davinci-001"):
  prompt = f"""The following text may fall inside one of this categories: {labels}, None

  {text}
  
  Category:"""
  response = openai.Completion.create(
    model=engine,
    prompt=prompt,
    temperature=0.7,
    max_tokens=10,
    stop=["\n"],
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
  )
  completion = response['choices'][0]['text'].strip()
  return completion
 
def gpt3_zero_shot_classification(text, labels):  
  completion = classificate_with_gpt3(text, labels, engine="text-babbage-001")
  return completion

# Gradio UI

with gr.Blocks(css=".gradio-container { background-color: white; }") as demo:
  gr.Markdown(f"# GPT-3 Zero shot classification app")
  with gr.Row():
    context = gr.Textbox(lines=3, label="Context", placeholder="Context here...")
  with gr.Row():
    labels = gr.Textbox(lines=3, label="Labels (Comma separated)", placeholder="Labels here...")
  btn = gr.Button(value="Analyze!", variant="primary")
  with gr.Row():
    out = gr.Textbox()
  btn.click(fn=gpt3_zero_shot_classification, inputs=[context,labels], outputs=[out])
  gr.Examples(
    [
      [ "On 22 February 2014, Ukrainian president Viktor Yanukovych was ousted from office as a result of the Euromaidan and the Revolution of Dignity, which broke out after his decision to reject the European Union–Ukraine Association Agreement and instead pursue closer ties with Russia and the Eurasian Economic Union. Shortly after Yanukovych's overthrow and exile to Russia, Ukraine's eastern and southern regions erupted with pro-Russia unrest.", "Entertainment, Business, Politics" ],
      [ "This dull recreation of the animated film doesn’t strive for anything more than what was contained in the original version of this film and actually delivers less.", "Entertainment, Business, Politics" ],
      [ "The third beta of iOS 16.1 that was released earlier this week expands the Adaptive Transparency feature introduced with the second-generation AirPods Pro to the original ‌AirPods Pro‌.", "Entertainment, Business, Politics" ],
      [ "To make this into soup, bring 3 1/2 cups water to a boil in a medium saucepan. Whisk in soup mix, reduce heat to low, and simmer for 5 minutes.", "Entertainment, Business, Politics" ]
    ],
    [context, labels],
    fn=gpt3_zero_shot_classification
  )

demo.launch(debug=True)