Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748491 unmodified lineimport gradio as grfrom groq import Groq# 安裝 groq 套件os.system('pip install groq')# 從環境變數中獲取API Keygroq_key = os.getenv('groq_key')# 設定API Keyclient = Groq(api_key=groq_key)def chat_with_groq(message):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)."},{"role": "user","content": message}],temperature=1,max_tokens=1024,top_p=1,stream=False,stop=None,)response = ""for chunk in completion:response += chunk.choices[0].delta.content or ""return responsewith 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_historymessage.submit(respond, [message, chatbot], [message, chatbot])demo.launch()1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445461 unmodified lineimport gradio as grfrom groq import 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",messages=[{"role": "system","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": user_input}],temperature=1.14,max_tokens=1024,top_p=1,stream=False,stop=None,)response = "".join(chunk.choices[0].delta.content or "" for chunk in completion)return response# 設定 Gradio Chatbot 介面iface = gr.Interface(fn=chatbot_response,inputs="text",outputs="text",title="Groq Chatbot",description="一個用於與 Groq API 互動的 Chatbot",)# 啟動 Gradio 應用程式if __name__ == "__main__":iface.launch()
app.py+21-24