Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718195 unmodified lines252627282930313233343536373839404142431 unmodified line45464748495051import 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}]5 unmodified linesmessages.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.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface1 unmodified linedemo = 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(1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162635 unmodified lines697071727374757677787980818283848586878889909192939495969798991001011021 unmodified line104105106107108109110import gradio as grfrom openai import OpenAIfrom huggingface_hub import InferenceClientfrom tenacity import retry, wait_random_exponential, stop_after_attemptOPENAI_KEY = os.getenv("OPENAI_KEY")client = OpenAI(api_key=OPEN_AI_KEY)def get_current_weather(location, unit="celsius"):"""Get the current weather in a given location"""if "taipei" in location.lower():return json.dumps({"location": "Taipei", "temperature": "10", "unit": unit})else:return json.dumps({"location": location, "temperature": "unknown"})@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):try:response = client.chat.completions.create(model=model,messages=messages,tools=tools,tool_choice=tool_choice,)return responseexcept Exception as e:print("Unable to generate ChatCompletion response")print(f"Exception: {e}")return etools = [{"type": "function","function": {"name": "get_current_weather","description": "Get the current weather","parameters": {"type": "object","properties": {"location": {"type": "string","description": "The city and state, e.g. San Francisco, CA",},"unit": {"type": "string","enum": ["celsius", "fahrenheit"],"description": "The temperature unit to use. Infer this from the users location.",},},"required": ["location", "unit"],},}}]def respond(message,history: list[tuple[str, str]],system_message,):messages = [{"role": "system", "content": system_message}]5 unmodified linesmessages.append({"role": "user", "content": message})response = chat_completion_request(messages, tools=tools, tool_choice='auto')response_message = response.choices[0].messagetool_calls = response_message.tool_callsif tool_calls:available_functions = {"get_current_weather": get_current_weather,}messages.append(response_message)for tool_call in tool_calls:function_name = tool_call.function.namefunction_to_call = available_functions[function_name]function_args = json.loads(tool_call.function.arguments)function_response = function_to_call(location=function_args.get("location"),unit=function_args.get("unit"),)messages.append({"tool_call_id": tool_call.id,"role": "tool","name": function_name,"content": function_response,})second_response = chat_completion_request(messages)print(second_response)return second_response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface1 unmodified linedemo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="", 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(
app.py+79-20