Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234569 unmodified lines1617181819202021222324252122272829302324253326272829303132333435363738373940414241424343444546474748495051525354495051565758595253546162635556575865596061import gradio as grimport osfrom groq import Groq
# Install the groq package if it is not installedtry:9 unmodified lines
client = Groq(api_key=groq_key)
class SimpleChatBot:class SpellChecker: def __init__(self): self.initial_prompt = [ { "role": "system", "content": "你是一個老師" } ] pass
def get_response(self, message, chat_history): messages = self.initial_prompt + chat_history messages.append({"role": "user", "content": message}) def check_spelling(self, text): completion = client.chat.completions.create( model="llama3-8b-8192", messages=messages, messages=[ { "role": "system", "content": "你是一個老師,請幫我挑錯字。" }, { "role": "user", "content": text } ], temperature=1, max_tokens=1024, top_p=1, stream=True, stream=False, stop=None, ) response_content = "" for chunk in completion: response_content += chunk.choices[0].delta.content or "" response_content = completion.choices[0].message["content"] return response_content
chatbot = SimpleChatBot()spell_checker = SpellChecker()
def respond(message, chat_history): chat_history = [{"role": entry["role"], "content": entry["content"]} for entry in chat_history] response = chatbot.get_response(message, chat_history) chat_history.append({"role": "user", "content": message}) chat_history.append({"role": "assistant", "content": response}) return chat_history, ""def check_spelling_interface(text): return spell_checker.check_spelling(text)
with gr.Blocks(title="簡單的Gradio聊天機器人") as demo: gr.Markdown("# 簡單的Gradio聊天機器人") chatbot_interface = gr.Chatbot(type="messages")with gr.Blocks(title="中文錯字檢查") as demo: gr.Markdown("# 中文錯字檢查") with gr.Row(): user_input = gr.Textbox(placeholder="輸入訊息...", label="你的訊息") send_button = gr.Button("發送") input_text = gr.Textbox(placeholder="輸入全文...", label="你的文本", lines=10) check_button = gr.Button("檢查錯字") output_text = gr.Textbox(label="錯字列表", lines=10) send_button.click(respond, inputs=[user_input, chatbot_interface], outputs=[chatbot_interface, user_input]) check_button.click(check_spelling_interface, inputs=input_text, outputs=output_text) demo.launch(share=True)app.py+25-31