Rookiehhh commited on
Commit ·
2c68e5d
1
Parent(s): 1895c34
init
Browse filesFiles changed (3)
- .gradio/certificate.pemAdded+31
- app.py+125-64
- requirements.txt+6-1
12345678910111213141516171819202122232425262728293031-----BEGIN CERTIFICATE-----MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAwTzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2VhcmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJuZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBYMTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygch77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6UA5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sWT8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyHB5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UCB5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUvKBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWnOlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTnjh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbwqHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CIrU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkqhkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZLubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KKNFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7UrTkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdCjNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVcoyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPAmRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57demyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=-----END CERTIFICATE-----12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import gradio as grfrom huggingface_hub import InferenceClient
"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p,): messages = [{"role": "system", "content": system_message}]
for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content
response += token yield response
"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface( respond, additional_inputs=[ gr.Textbox(value="You are a friendly Chatbot.", label="System message"), gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ],)
if __name__ == "__main__": demo.launch()import os import gradio as gr from langchain import OpenAI, LLMChain from langchain.prompts import PromptTemplate from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI
# 确保安装了 python-dotenv 以加载 .env 文件中的环境变量 from dotenv import load_dotenv
load_dotenv()
# 获取 OpenAI API Key openai_api_key = os.getenv("OPENAI_API_KEY") if not openai_api_key: raise ValueError("未找到 OPENAI_API_KEY 环境变量。请确保已正确设置。")
# 配置 LangChain 的 ChatOpenAI llm = ChatOpenAI( openai_api_key=openai_api_key, model_name="gpt-3.5-turbo", # 您也可以选择其他模型,比如 "gpt-4" temperature=0.7, max_tokens=150 )
# 定义提示模板 prompt_template = """ 你是一个乐于助人的 AI 助手。请根据以下对话内容进行回复:
{history} 用户: {input}
AI: """
# 创建提示模板对象 prompt = PromptTemplate( input_variables=["history", "input"], template=prompt_template )
# 创建记忆模块 memory = ConversationBufferMemory( memory_key="history", return_messages=True )
# 创建 LLMChain chain = LLMChain( llm=llm, prompt=prompt, memory=memory )
def generate_response(input_text): """ 使用 LangChain 生成 AI 回复。
Args: input_text (str): 用户输入的消息
Returns: str: AI 生成的回复 """ response = chain.run(input_text) return response
def reset_chat(): """ 重置聊天记录。
Returns: str: 重置后的聊天记录 """ memory.clear() return ""
# 使用 Gradio Blocks 构建界面 with gr.Blocks() as demo: gr.Markdown("# LangChain + Gradio GPT 聊天机器人") gr.Markdown("这是一个示例,使用了 LangChain 的模板和记忆模块。") # 聊天组件 chatbot = gr.Chatbot() # 用户输入框 user_input = gr.Textbox( show_label=False, placeholder="输入你的问题并按回车...", lines=1 ) # 按钮 send_button = gr.Button("发送") reset_button = gr.Button("重置聊天") # 记录对话历史 def respond(message, chat_history): """ 处理用户输入并生成回复。
Args: message (str): 用户输入 chat_history (list): 当前聊天记录
Returns: list: 更新后的聊天记录 """ if message: # 生成 AI 回复 ai_reply = generate_response(message) # 更新聊天记录 chat_history.append((message, ai_reply)) return chat_history
# 监听发送按钮和回车事件 send_button.click(respond, inputs=[user_input, chatbot], outputs=chatbot) user_input.submit(respond, inputs=[user_input, chatbot], outputs=chatbot) # 重置聊天按钮 reset_button.click(reset_chat, outputs=chatbot)
# 启动 Gradio 界面 if __name__ == "__main__": demo.launch()1123456huggingface_hub==0.25.2huggingface_hub==0.25.2langchain==0.3.7langchain-community==0.3.7langchain-core==0.3.19python-dotenvgradio.gradio/certificate.pemAdded+31
app.py+125-64
requirements.txt+6-1