Sangmin commited on
Commit
4be36ad
1 Parent(s): 1a15e30

Initial commit

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ ##Bloom Inference API
6
+ API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
7
+ HF_TOKEN = os.environ["HF_TOKEN"]
8
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
9
+
10
+ def write_essay(sample_topic, user_topic):
11
+ if len(sample_topic) == 0:
12
+ sample_topic = user_topic
13
+ p = "Instruction: Write a five-paragraph essay for the following topic:\nSuggested Length: more than 120 words\nTOPIC: " + sample_topic + "\n\nESSAY:"
14
+ try:
15
+ json_ = {"inputs": p,
16
+ "parameters":
17
+ {
18
+ "top_p": 0.9,
19
+ "temperature": 1.1,
20
+ "max_new_tokens": 250,
21
+ "return_full_text": True
22
+ }, "options":
23
+ {
24
+ "use_cache": True,
25
+ "wait_for_model":True
26
+ },}
27
+ response = requests.post(API_URL, headers=headers, json=json_)
28
+ print(f"Response is : {response}")
29
+ output = response.json()
30
+ print(f"output is : {output}") #{output}")
31
+ output_tmp = output[0]['generated_text']
32
+ print(f"output_tmp is: {output_tmp}")
33
+ #print(response)
34
+ return output_tmp
35
+ except:
36
+ return "Invalid Query"
37
+
38
+ demo = gr.Blocks()
39
+
40
+ with demo:
41
+ gr.Markdown("<h3><center>人工知能から学ぶ、英検作文へのヒント</center></h3>")
42
+ gr.Markdown("<h4><center>使い方:トピックを選んで画面左下にある「エッセイ作成」ボタンをクリック</center></h4>")
43
+ gr.Markdown("<h4><center>※重要:費用があまりにも大きくなってきましたのでサービスを中止させていただきます。</center></h4>")
44
+ gr.Markdown("<h4><center>専用のAPIキーを取得してご利用ください。</center></h4>")
45
+ gr.Markdown("<center>Created by Choimirai School</center>")
46
+
47
+ with gr.Row():
48
+ sample_topic = gr.Radio([
49
+ "Will humans live on other planets someday?",
50
+ "Japan should become a completely cashless society.",
51
+ "Global overpopulation is a serious threat to the future of humankind.",
52
+ "Improving relations with other Asian nations should be a priority for the Japanese government.",
53
+ "Can renewable energy sources replace fossil fuels?",
54
+ "Should democratic nations actively promote the spread of democracy to nondemocratic nations?",
55
+ "Agree or disagree, Infectious diseases will become a bigger problem in the coming decades.", ], label= "Choose a sample TOPIC")
56
+
57
+ user_topic = gr.Textbox(label="Or, write your own topic for Eiken essay.", value="Will fossil fuels such as oil and gas still be the world's main source of energy in the coming decades?")
58
+
59
+ with gr.Row():
60
+ written_essay = gr.inputs.Textbox(lines=20, label="Sample Essay written by GPT-3")
61
+
62
+ b1 = gr.Button("エッセイ作成")
63
+ b1.click(write_essay, inputs = [sample_topic, user_topic], outputs = written_essay)
64
+
65
+ with gr.Row():
66
+ gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=sangmin_eiken-essay-with-gpt3)")
67
+
68
+ demo.launch(enable_queue=True, debug=True)