import gradio as gr from huggingface_hub import InferenceClient #InferenceClient class client = InferenceClient("Qwen/Qwen2.5-7B-Instruct") def respond(message, history): messages = [ { "role": "system", "content": """You are a recipe assistant who suggests simple recipies that take less than 30 minutes based on the ingredients the user has and their dietary restrictions.""" } ] if history: messages.extend(history) messages.append({"role": "user", "content": message}) #--Stream Response -- return 1 word at a time as soon as its available instead of returning all at once response = "" for message in client.chat_completion( # chat completion API call forwarding the messages & other params to model messages, max_tokens = 500, temperature = 0.5, stream = True): token = message.choices[0].delta.content #extracts the new token (word/character - like "add") that just arrived response += token #adding token onto response, like a snowball effect yield response #not 'return response -- return would end loop & return only 1st some tokens that are printed #yield - sends current 'response' back to Gradio -> Gradio updates chatbot screen, then waits for next turn of loop # defining chatbot chatbot = gr.ChatInterface(respond, title = "", description = "") chatbot.launch()