youngtsai commited on
Commit
1d4a655
·
verified ·
1 Parent(s): c961de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -7
app.py CHANGED
@@ -1,12 +1,51 @@
 
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # 創建語音識別的 pipeline
5
- asr = pipeline("automatic-speech-recognition")
 
 
 
 
6
 
7
- def recognize_speech(audio):
8
- result = asr(audio)
9
- return result['text']
10
 
11
- iface = gr.Interface(fn=recognize_speech, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text", title="語音轉文字應用", description="請說話,然後按下停止按鈕進行文字轉換。")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  iface.launch()
 
1
+ import os
2
  import gradio as gr
 
3
 
4
+ # 確保 groq 套件可用
5
+ try:
6
+ from groq import Groq
7
+ except ImportError:
8
+ os.system('pip install groq')
9
+ from groq import Groq
10
 
11
+ # 初始化 Groq 客戶端
12
+ client = Groq()
 
13
 
14
+ # 定義 chatbot 回應的函數
15
+ def chat_with_groq(user_input):
16
+ completion = client.chat.completions.create(
17
+ model="llama-3.1-70b-versatile",
18
+ messages=[
19
+ {
20
+ "role": "system",
21
+ "content": "我是國文老師,專門批改國小作文"
22
+ },
23
+ {
24
+ "role": "user",
25
+ "content": user_input
26
+ }
27
+ ],
28
+ temperature=1,
29
+ max_tokens=1024,
30
+ top_p=1,
31
+ stream=True,
32
+ stop=None,
33
+ )
34
+
35
+ response = ""
36
+ for chunk in completion:
37
+ response += chunk.choices[0].delta.content or ""
38
+
39
+ return response
40
+
41
+ # 使用 Gradio 創建 chatbot
42
+ iface = gr.Interface(
43
+ fn=chat_with_groq,
44
+ inputs=gr.inputs.Textbox(label="請輸入您的問題:"),
45
+ outputs=gr.outputs.Textbox(label="回應:"),
46
+ title="國文老師 Chatbot",
47
+ description="這是一個專門批改國小作文的 Chatbot"
48
+ )
49
+
50
+ # 啟動應用
51
  iface.launch()