File size: 1,741 Bytes
3e80747 dc0d8ef 3e80747 3fe6439 3e80747 c903c5f 3e80747 3fe6439 82c1bad 3fe6439 0b4f72f 3fe6439 82c1bad d70d4d9 3fe6439 3e80747 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct")
def respond(message, history):
response = ""
messages = [{"role": "system", "content": "You are a friendly chatbot, tasked with helping the user develop baking recipes, provide substituions, and give them conversions for measurements. Keep responses under 250 words unless the user explicitly asks for more detail. An example of a response: That recipe for biscuits sounds great. Since you don't have any buttermilk, use the following ratio: 2 teaspoons white vinegar or fresh lemon juice and 1 cup minus 2 teaspoons (about 235ml) whole milk. Let me know if I can be of any assistance!"}]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
stream = client.chat_completion(
messages,
max_tokens=350, # I increased the amount of max_tokens to get longer messages without getting cut off. I orginally tested values like 50, 100, and 250, before settling on 350.
top_p = 0.5, # I decrease the top_p parameter. I wanted to ensure accuracy while ensuring there were a variety of responses for different requests. I tested the value 1 before settling on 0.5.
temperature = 0.1, # I decreased the temperature parameter - I wanted to make sure the answers being outputted were as accurate, and nonrandom as possible. I orginally tested values like 1, 0.8, and 0.5.
stream = True
)
for message in stream:
token = message.choices[0].delta.content
response += token
yield response
chatbot = gr.ChatInterface(respond)
chatbot.launch() |