Princeps3007/Jupiter
0
1import os2import gradio as gr3from huggingface_hub import InferenceClient4 5"""6For more information on `huggingface_hub` Inference API support,7please check the docs:8https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference9"""10 11# Retrieve your token from the environment variable you set in the Space's secrets12token = os.getenv("HF_API_TOKEN")13 14# Instantiate the client using your token15client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)16 17def respond(18 message,19 history: list[tuple[str, str]],20 system_message,21 max_tokens,22 temperature,23 top_p,24):25 messages = [{"role": "system", "content": system_message}]26 27 for val in history:28 if val[0]:29 messages.append({"role": "user", "content": val[0]})30 if val[1]:31 messages.append({"role": "assistant", "content": val[1]})32 33 messages.append({"role": "user", "content": message})34 35 response = ""36 37 for msg in client.chat_completion(38 messages,39 max_tokens=max_tokens,40 stream=True,41 temperature=temperature,42 top_p=top_p,43 ):44 token_content = msg.choices[0].delta.content45 response += token_content46 yield response47 48"""49For information on how to customize the ChatInterface,50peruse the gradio docs: https://www.gradio.app/docs/chatinterface51"""52demo = gr.ChatInterface(53 respond,54 additional_inputs=[55 gr.Textbox(value="You are a friendly Chatbot.", label="System message"),56 gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),57 gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),58 gr.Slider(59 minimum=0.1,60 maximum=1.0,61 value=0.95,62 step=0.05,63 label="Top-p (nucleus sampling)",64 ),65 ],66)67 68if __name__ == "__main__":69 demo.launch()70 