Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import osimport gradio as grtry:from groq import Groqexcept ImportError:os.system('pip install groq')from 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 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)."},{"role": "user","content": user_input}],temperature=1.14,max_tokens=1024,top_p=1,stream=False,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_contentreturn 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()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import osimport gradio as grfrom groq import Groq# 安裝 groq 套件os.system('pip install groq')# 從環境變量中獲取 Groq 的 API keygroq_key = os.getenv("groq_key")# 初始化 Groq 客戶端client = Groq(api_key=groq_key)# 定義聊天機器人回應邏輯def chatbot_response(user_input):completion = client.chat.completions.create(model="llama-3.1-8b-instant",messages=[{"role": "system","content": "你是一個有幽默感的聊天機器人,你擅長用詼諧的方式來討論嚴肅的事情,尤其是政治方面的議題。你使用的語言是繁體中文(zh-tw)。"},{"role": "user","content": user_input}],temperature=1.18,max_tokens=1024,top_p=1,stream=True,stop=None,)response = ""for chunk in completion:response += chunk.choices[0].delta.content or ""return response# 建立 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, historyinterface = gr.Interface(fn=respond_to_user,inputs=[input_text, chatbot],outputs=[chatbot, chatbot],live=True,allow_flagging='never',)# 啟動應用if __name__ == "__main__":interface.launch()
app.py+30-27