import gradio as gr
import requests
import os
##Bloom Inference API
API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"
HF_TOKEN = os.environ["HF_TOKEN"]
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
def write_essay(sample_topic, user_topic):
if len(sample_topic) == 0:
sample_topic = user_topic
p = "Instruction: Write a five-paragraph essay for the following topic:\nSuggested Length: more than 120 words\nTOPIC: " + sample_topic + "\n\nESSAY:"
try:
json_ = {"inputs": p,
"parameters":
{
"top_p": 0.9,
"temperature": 1.1,
"max_new_tokens": 250,
"return_full_text": True
}, "options":
{
"use_cache": True,
"wait_for_model":True
},}
response = requests.post(API_URL, headers=headers, json=json_)
print(f"Response is : {response}")
output = response.json()
print(f"output is : {output}") #{output}")
output_tmp = output[0]['generated_text']
print(f"output_tmp is: {output_tmp}")
#print(response)
return output_tmp
except:
return "Invalid Query"
demo = gr.Blocks()
with demo:
gr.Markdown("
人工知能から学ぶ、英検作文へのヒント
")
gr.Markdown("使い方:トピックを選んで画面左下にある「エッセイ作成」ボタンをクリック
")
gr.Markdown("※重要:費用があまりにも大きくなってきましたのでサービスを中止させていただきます。
")
gr.Markdown("専用のAPIキーを取得してご利用ください。
")
gr.Markdown("Created by Choimirai School")
with gr.Row():
sample_topic = gr.Radio([
"Will humans live on other planets someday?",
"Japan should become a completely cashless society.",
"Global overpopulation is a serious threat to the future of humankind.",
"Improving relations with other Asian nations should be a priority for the Japanese government.",
"Can renewable energy sources replace fossil fuels?",
"Should democratic nations actively promote the spread of democracy to nondemocratic nations?",
"Agree or disagree, Infectious diseases will become a bigger problem in the coming decades.", ], label= "Choose a sample TOPIC")
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?")
with gr.Row():
written_essay = gr.inputs.Textbox(lines=20, label="Sample Essay written by GPT-3")
b1 = gr.Button("エッセイ作成")
b1.click(write_essay, inputs = [sample_topic, user_topic], outputs = written_essay)
with gr.Row():
gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=sangmin_eiken-essay-with-gpt3)")
demo.launch(enable_queue=True, debug=True)