CoolFace
Apppublic

cdc-hf/test-space

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py70 linesDownload Raw Back to root
1import gradio as gr2import os3from langchain_openai import AzureChatOpenAI4from huggingface_hub import InferenceClient5 6"""7For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference8"""9client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")10os.environ["AZURE_OPENAI_API_KEY"] = os.environ.get('API_KEY')11os.environ["AZURE_OPENAI_ENDPOINT"] = os.environ.get('ENDPOINT')12 13llm = AzureChatOpenAI(14    api_version="2024-05-01-preview",15    model="GPT-4"16)17 18def respond(19    message,20    history: list[tuple[str, str]],21    system_message,22    max_tokens,23    temperature,24    top_p,25):26    messages = [{"role": "system", "content": system_message}]27 28    for val in history:29        if val[0]:30            messages.append({"role": "user", "content": val[0]})31        if val[1]:32            messages.append({"role": "assistant", "content": val[1]})33 34    messages.append({"role": "user", "content": message})35 36    response = ""37 38    response =  llm.invoke(39        messages,40        max_tokens=max_tokens,41        stream=True,42        temperature=temperature,43        top_p=top_p,44    ).content45    yield response46 47 48"""49For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface50"""51demo = gr.ChatInterface(52    respond,53    additional_inputs=[54        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),55        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),56        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),57        gr.Slider(58            minimum=0.1,59            maximum=1.0,60            value=0.95,61            step=0.05,62            label="Top-p (nucleus sampling)",63        ),64    ],65)66 67 68if __name__ == "__main__":69    demo.launch()70