Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345656789101189101411121314151617201819202124222324272526272829303334353132333438394041424344454647353637383940414243444549461 unmodified lineimport gradio as grfrom groq import Groq
# 安裝 groq 套件os.system('pip install groq')# 設定 API Keygroq_key = os.getenv("groq_key")
# 從環境變數中獲取API Keygroq_key = os.getenv('groq_key')
# 設定API Key# 初始化 Groq 客戶端client = Groq(api_key=groq_key)
def chat_with_groq(message):# 定義 Chatbot 回應函數def chatbot_response(user_input): completion = client.chat.completions.create( model="llama-3.1-70b-versatile", messages=[ { "role": "system", "content": "You have a humorous temperament, always discuss serious matters with a sense of humor. \nYou should always use Traditional Chinese (zh-tw)." "content": "You have a humorous temperament, and are capable of discussing serious matters with a sense of humor.\nYou speak Traditional Chinese(zh-tw)." }, { "role": "user", "content": message "content": user_input } ], temperature=1, temperature=1.14, max_tokens=1024, top_p=1, stream=False, stop=None, ) response = "" for chunk in completion: response += chunk.choices[0].delta.content or ""
response = "".join(chunk.choices[0].delta.content or "" for chunk in completion) return response
with gr.Blocks() as demo: chatbot = gr.Chatbot() message = gr.Textbox(placeholder="Type your message here...") def respond(message, chat_history): bot_response = chat_with_groq(message) chat_history.append((message, bot_response)) return "", chat_history message.submit(respond, [message, chatbot], [message, chatbot])# 設定 Gradio Chatbot 介面iface = gr.Interface( fn=chatbot_response, inputs="text", outputs="text", title="Groq Chatbot", description="一個用於與 Groq API 互動的 Chatbot",)
# 啟動 Gradio 應用程式if __name__ == "__main__":demo.launch() iface.launch()app.py+21-24