Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,82 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
def chat_with_groq(user_message):
|
22 |
-
"""與 Groq 模型進行互動"""
|
23 |
-
try:
|
24 |
-
# 調用 API
|
25 |
-
completion = client.chat.completions.create(
|
26 |
-
model="llama3-groq-70b-8192-tool-use-preview",
|
27 |
-
messages=[
|
28 |
-
{
|
29 |
-
"role": "system",
|
30 |
-
"content": "你是一位虛擬助手,專注於回答使用者的提問,提供準確而簡潔的資訊。"
|
31 |
-
},
|
32 |
-
{"role": "user", "content": user_message}
|
33 |
-
],
|
34 |
-
temperature=0.5,
|
35 |
-
max_tokens=1024,
|
36 |
-
top_p=0.65,
|
37 |
-
stream=True
|
38 |
-
)
|
39 |
-
|
40 |
-
# 解析流式回應
|
41 |
-
response = ""
|
42 |
-
for chunk in completion:
|
43 |
-
delta_content = chunk.choices[0].delta.get("content", "")
|
44 |
-
response += delta_content
|
45 |
-
return response
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
gr.Markdown("### Groq Chatbot")
|
60 |
-
chatbot = gr.Chatbot()
|
61 |
-
user_input = gr.Textbox(placeholder="輸入你的訊息...")
|
62 |
-
submit_button = gr.Button("送出")
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
return demo
|
67 |
|
68 |
-
# 啟動應用程式
|
69 |
if __name__ == "__main__":
|
70 |
-
|
71 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def create_idiom_game():
|
4 |
+
# 建立 Gradio 介面
|
5 |
+
with gr.Blocks() as demo:
|
6 |
+
gr.Markdown("# 成語對對碰")
|
7 |
+
|
8 |
+
# 嵌入 HTML 和 JavaScript
|
9 |
+
gr.HTML("""
|
10 |
+
<div id="game-root"></div>
|
11 |
+
<script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
|
12 |
+
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
|
13 |
+
<script>
|
14 |
+
const idiomDatabase = [
|
15 |
+
{ mainIdiom: '急如星火', synonyms: ['迫在眉睫', '刻不容緩'], meaning: '形容事情非常緊急' },
|
16 |
+
{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },
|
17 |
+
{ mainIdiom: '一丘之貉', synonyms: ['沆瀣一氣', '狐群狗黨'], meaning: '形容品性相同的壞人' }
|
18 |
+
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
function IdiomGame() {
|
21 |
+
const [currentIndex, setCurrentIndex] = React.useState(0);
|
22 |
+
const [score, setScore] = React.useState(0);
|
23 |
+
const [selected, setSelected] = React.useState([]);
|
24 |
|
25 |
+
const current = idiomDatabase[currentIndex];
|
26 |
+
|
27 |
+
const handleSelect = (answer) => {
|
28 |
+
if (selected.includes(answer)) return;
|
29 |
+
|
30 |
+
if (current.synonyms.includes(answer)) {
|
31 |
+
const newSelected = [...selected, answer];
|
32 |
+
setSelected(newSelected);
|
33 |
+
|
34 |
+
if (newSelected.length === 2) {
|
35 |
+
setScore(score + 10);
|
36 |
+
setTimeout(() => {
|
37 |
+
setCurrentIndex((currentIndex + 1) % idiomDatabase.length);
|
38 |
+
setSelected([]);
|
39 |
+
}, 1000);
|
40 |
+
}
|
41 |
+
}
|
42 |
+
};
|
43 |
|
44 |
+
const options = [...current.synonyms, '望穿秋水', '守株待兔', '揮金如土'].sort(() => Math.random() - 0.5);
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
return React.createElement('div', { style: { padding: '20px' } },
|
47 |
+
React.createElement('div', { style: { backgroundColor: '#f0f0f0', padding: '20px', marginBottom: '20px' } },
|
48 |
+
React.createElement('h2', null, current.mainIdiom),
|
49 |
+
React.createElement('p', null, current.meaning)
|
50 |
+
),
|
51 |
+
React.createElement('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: '10px' } },
|
52 |
+
options.map((option, index) =>
|
53 |
+
React.createElement('button', {
|
54 |
+
key: index,
|
55 |
+
onClick: () => handleSelect(option),
|
56 |
+
style: {
|
57 |
+
padding: '10px',
|
58 |
+
backgroundColor: selected.includes(option) ? '#4CAF50' : 'white',
|
59 |
+
color: selected.includes(option) ? 'white' : 'black',
|
60 |
+
border: '1px solid #ddd'
|
61 |
+
}
|
62 |
+
}, option)
|
63 |
+
)
|
64 |
+
),
|
65 |
+
React.createElement('div', { style: { marginTop: '20px' } },
|
66 |
+
`分數: ${score}`
|
67 |
+
)
|
68 |
+
);
|
69 |
+
}
|
70 |
|
71 |
+
ReactDOM.render(
|
72 |
+
React.createElement(IdiomGame),
|
73 |
+
document.getElementById('game-root')
|
74 |
+
);
|
75 |
+
</script>
|
76 |
+
""")
|
77 |
+
|
78 |
return demo
|
79 |
|
|
|
80 |
if __name__ == "__main__":
|
81 |
+
demo = create_idiom_game()
|
82 |
+
demo.launch()
|