Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
# Install the groq package if it is not installed
|
| 5 |
try:
|
|
@@ -15,53 +16,46 @@ if not groq_key:
|
|
| 15 |
|
| 16 |
client = Groq(api_key=groq_key)
|
| 17 |
|
| 18 |
-
class
|
| 19 |
def __init__(self):
|
| 20 |
-
|
| 21 |
-
{
|
| 22 |
-
"role": "system",
|
| 23 |
-
"content": "你是一個老師"
|
| 24 |
-
}
|
| 25 |
-
]
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
messages = self.initial_prompt + chat_history
|
| 29 |
-
messages.append({"role": "user", "content": message})
|
| 30 |
-
|
| 31 |
completion = client.chat.completions.create(
|
| 32 |
model="llama3-8b-8192",
|
| 33 |
-
messages=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
temperature=1,
|
| 35 |
max_tokens=1024,
|
| 36 |
top_p=1,
|
| 37 |
-
stream=
|
| 38 |
stop=None,
|
| 39 |
)
|
| 40 |
|
| 41 |
-
response_content = ""
|
| 42 |
-
for chunk in completion:
|
| 43 |
-
response_content += chunk.choices[0].delta.content or ""
|
| 44 |
|
| 45 |
return response_content
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
-
def
|
| 50 |
-
|
| 51 |
-
response = chatbot.get_response(message, chat_history)
|
| 52 |
-
chat_history.append({"role": "user", "content": message})
|
| 53 |
-
chat_history.append({"role": "assistant", "content": response})
|
| 54 |
-
return chat_history, ""
|
| 55 |
|
| 56 |
-
with gr.Blocks(title="
|
| 57 |
-
gr.Markdown("#
|
| 58 |
-
|
| 59 |
-
chatbot_interface = gr.Chatbot(type="messages")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
|
| 67 |
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
# Install the groq package if it is not installed
|
| 6 |
try:
|
|
|
|
| 16 |
|
| 17 |
client = Groq(api_key=groq_key)
|
| 18 |
|
| 19 |
+
class SpellChecker:
|
| 20 |
def __init__(self):
|
| 21 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def check_spelling(self, text):
|
|
|
|
|
|
|
|
|
|
| 24 |
completion = client.chat.completions.create(
|
| 25 |
model="llama3-8b-8192",
|
| 26 |
+
messages=[
|
| 27 |
+
{
|
| 28 |
+
"role": "system",
|
| 29 |
+
"content": "你是一個老師,請幫我挑錯字。"
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"role": "user",
|
| 33 |
+
"content": text
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
temperature=1,
|
| 37 |
max_tokens=1024,
|
| 38 |
top_p=1,
|
| 39 |
+
stream=False,
|
| 40 |
stop=None,
|
| 41 |
)
|
| 42 |
|
| 43 |
+
response_content = completion.choices[0].message["content"]
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return response_content
|
| 46 |
|
| 47 |
+
spell_checker = SpellChecker()
|
| 48 |
|
| 49 |
+
def check_spelling_interface(text):
|
| 50 |
+
return spell_checker.check_spelling(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
with gr.Blocks(title="中文錯字檢查") as demo:
|
| 53 |
+
gr.Markdown("# 中文錯字檢查")
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
input_text = gr.Textbox(placeholder="輸入全文...", label="你的文本", lines=10)
|
| 56 |
+
check_button = gr.Button("檢查錯字")
|
| 57 |
+
output_text = gr.Textbox(label="錯字列表", lines=10)
|
| 58 |
|
| 59 |
+
check_button.click(check_spelling_interface, inputs=input_text, outputs=output_text)
|
| 60 |
|
| 61 |
demo.launch(share=True)
|