Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
import pyperclip | |
DEFAULT_MODEL_NAME = "gpt-3.5-turbo-0613" | |
openai.api_key = "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" # 会社用 | |
def init_state(state): | |
if (len(state["message_history"]) == 0): | |
state["message_history"] = [] | |
if (len(state["summarize_history"]) == 0): | |
state["summarize_history"] = [] | |
if (len(state["title_history"]) == 0): | |
state["title_history"] = "" | |
def make_return_message(state): | |
init_state(state) | |
if len(state["summarize_history"]) > 0: | |
message_string = '\n'.join(state["message_history"]) | |
summarize_string = '\n'.join(state["summarize_history"]) | |
title = state["title_history"] | |
return f""" | |
# {title} | |
## 要約 | |
{summarize_string} | |
## 本文 | |
{message_string} | |
""" | |
else: | |
message_string = '\n'.join(state["message_history"]) | |
return f""" | |
{message_string} | |
""" | |
def summarize_text_from_text(state,txt): | |
init_state(state) | |
state["message_history"] = txt | |
return summarize_text(state) | |
def summarize_text(state): | |
def modify_text(state): | |
message_string = '\n'.join(state["message_history"]) | |
prompt = f""" | |
# 命令 | |
以下の文章の誤字脱字を修正して、適切に段落をつけてください。 | |
出力は修正後の文章だけにしてください。 | |
# 修正対象の文章 | |
{message_string} | |
# Output | |
""" | |
response = openai.ChatCompletion.create( | |
# model="gpt-3.5-turbo-0613", | |
model="gpt-3.5-turbo-16k", | |
# max_tokens=1000, | |
messages=[ | |
{"role": "user", "content": prompt} | |
] | |
) | |
assistant_msg = response["choices"][0]["message"]["content"] | |
print(assistant_msg) | |
state["message_history"] = assistant_msg.split('\n') | |
def make_title(state): | |
message_string = '\n'.join(state["message_history"]) | |
prompt = f""" | |
# 命令 | |
以下の文章に、全体の内容を分かりやすく表現する適切なタイトル文をつけてください。 | |
タイトル文は20文字以内にしてください。 | |
出力はタイトル文だけにしてください。 | |
# 文章 | |
{message_string} | |
# Output | |
""" | |
response = openai.ChatCompletion.create( | |
# model="gpt-3.5-turbo-0613", | |
model="gpt-3.5-turbo-16k", | |
# max_tokens=1000, | |
messages=[ | |
{"role": "user", "content": prompt} | |
] | |
) | |
assistant_msg = response["choices"][0]["message"]["content"] | |
print(assistant_msg) | |
state["title_history"] = assistant_msg | |
print(state["title_history"]) | |
init_state(state) | |
modify_text(state) | |
make_title(state) | |
message_string = '\n'.join(state["message_history"]) | |
prompt = f""" | |
# 命令 | |
以下の文章を箇条書きで、5行で要約してください。 | |
出力は修正後の文章だけにしてください。 | |
# 要約対象の文章 | |
{message_string} | |
# Output | |
""" | |
response = openai.ChatCompletion.create( | |
# model="gpt-3.5-turbo-0613", | |
model="gpt-3.5-turbo-16k", | |
# max_tokens=1000, | |
messages=[ | |
{"role": "user", "content": prompt} | |
] | |
) | |
assistant_msg = response["choices"][0]["message"]["content"] | |
print(assistant_msg) | |
state["summarize_history"] = assistant_msg.split('\n') | |
return make_return_message(state) | |
def memory_clear(state): | |
if (len(state["message_history"]) > 0): | |
state["message_history"].clear() | |
else: | |
state["message_history"] = [] | |
if (len(state["summarize_history"]) > 0): | |
state["summarize_history"].clear() | |
else: | |
state["summarize_history"] = [] | |
return "" | |
def copy_clipborad(text): | |
pyperclip.copy(text) | |
def transcribe(audio,state): | |
if (audio is None): | |
return | |
init_state(state) | |
audio_file = open(audio, "rb") | |
transcription = openai.Audio.transcribe("whisper-1", audio_file,language="ja") | |
state["message_history"].append(transcription["text"]) | |
print(transcription["text"]) | |
# print(message_history.toString()) | |
return make_return_message(state) | |
user_db = {"a": "aa", "yourmystar": "amazing"} | |
def auth(user_name, password): | |
return user_db.get(user_name) == password | |
with gr.Blocks() as app: | |
state = gr.State({ | |
"message_history" : [], | |
"title_history" : [], | |
"summarize_history" : [] | |
}) | |
gr.Markdown( | |
""" | |
# TextWriter AI | |
音声文字おこし&要約ツール。 | |
""") | |
with gr.Tabs(): | |
with gr.TabItem("1.音声文字起こし→要約"): | |
# gr.Markdown( | |
# """ | |
# #### ①「音声文字起こし→要約」の使い方 | |
# 1. 「**Record from microphone**」を押すと録音が始まります。メモしたいことを話してください | |
# 2. 「**Stop recording**」で録音を止めます | |
# 3. 文字起こしがされます | |
# 4. 1〜3を繰り返していくことで、音声を文字起こししていきます | |
# 5. 「**要約をつける**」を押すと、ChatGPTが誤字脱字を修正し、要約をつけてくれます | |
# ※ブラウザ更新すると文章が消えるので、忘れずにコピペしてください<br> | |
# ※スマホだとクリップボードがうまく動作していませんm(_ _)m<br> | |
# ※要約できるのは1万文字くらいまでです""") | |
inputs = gr.Audio(source="microphone", type="filepath", show_label=True,label="ボタンを押して話す") | |
debug_field = gr.Textbox(show_label=False,label="", placeholder="ここに文字起こしが出ます") | |
inputs.change(fn=transcribe, inputs=[inputs,state], outputs=[debug_field]) | |
inputs.change(lambda :None, None, inputs) | |
btn_summerize = gr.Button("要約する") | |
btn_summerize.click(fn=summarize_text, inputs=state, outputs=[debug_field]) | |
btn_summerize = gr.Button("クップボードにコピー") | |
btn_summerize.click(fn=copy_clipborad, inputs=debug_field, outputs=None) | |
# btn_memory_clear = gr.Button("test") | |
# btn_memory_clear.click(fn=test_m, inputs=state, outputs=debug_field) | |
with gr.TabItem("2.文章コピペ→要約"): | |
# gr.Markdown( | |
# """ | |
# #### ②「文章コピペ→要約」の使い方 | |
# 1. 「**ここに文字起こしが出ます or 要約したい文章をここに貼り付けてください**」の欄に要約したい文章をコピペ | |
# 2. 「**コピペ結果に要約をつける**」を押すと、ChatGPTが誤字脱字を修正し、要約をつけてくれます | |
# ※ブラウザ更新すると文章が消えるので、忘れずにコピペしてください<br> | |
# ※スマホだとクリップボードがうまく動作していませんm(_ _)m<br> | |
# ※要約できるのは1万文字くらいまでです""") | |
text_copipe_input = gr.Textbox(show_label=False,label="", placeholder="要約したい文章をここに貼り付けてください") | |
btn_summerize = gr.Button("要約する") | |
btn_summerize.click(fn=summarize_text_from_text, inputs=[state,text_copipe_input], outputs=[text_copipe_input]) | |
btn_summerize = gr.Button("クップボードにコピー") | |
btn_summerize.click(fn=copy_clipborad, inputs=debug_field, outputs=None) | |
app.launch(auth=auth) |