Spaces:
Runtime error
Runtime error
Update app.py
Browse files12343567456710891011121316141516191718192023212223242526273028293033313233363734353640414243443738394047484950515253414243444546474849505152535455565756585958606162import osimport gradio as grtry: from groq import Groqfrom groq import Groqexcept ImportError: os.system('pip install groq') from groq import Groq
# 安裝 groq 套件os.system('pip install groq')
# 設定 API Key# 從環境變量中獲取 Groq 的 API keygroq_key = os.getenv("groq_key")
# 初始化 Groq 客戶端client = Groq(api_key=groq_key)
# 定義 Chatbot 回應函數# 定義聊天機器人回應邏輯def chatbot_response(user_input): completion = client.chat.completions.create( model="llama-3.1-70b-versatile", model="llama-3.1-8b-instant", messages=[ { "role": "system", "content": "You are a chatbot. You have a humorous temperament, and are capable of discussing serious matters with a sense of humor.\nYou speak Traditional Chinese(zh-tw)." "content": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)。" }, { "role": "user", "content": user_input } ], temperature=1.14, temperature=1.18, max_tokens=1024, top_p=1, stream=False, stream=True, stop=None, )
# 檢查並解析回應 response = "" for chunk in completion: if hasattr(chunk, 'choices') and len(chunk.choices) > 0: delta_content = getattr(chunk.choices[0], 'delta', {}).get('content', "") if delta_content: response += delta_content
response += chunk.choices[0].delta.content or "" return response
# 設定 Gradio Chatbot 介面iface = gr.Interface( fn=chatbot_response, inputs="text", outputs="text", title="Groq Chatbot", description="一個用於與 Groq API 互動的 Chatbot",# 建立 Gradio 聊天機器人介面chatbot = gr.Chatbot()input_text = gr.inputs.Textbox(placeholder="輸入你的訊息...")
def respond_to_user(input_text, history): response = chatbot_response(input_text) history.append((input_text, response)) return history, history
interface = gr.Interface( fn=respond_to_user, inputs=[input_text, chatbot], outputs=[chatbot, chatbot], live=True, allow_flagging='never',)
# 啟動 Gradio 應用程式# 啟動應用if __name__ == "__main__": iface.launch() interface.launch()
app.py+30-27