pythonlady/Codecat
0
1import gradio as gr2from huggingface_hub import InferenceClient3 4client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")5 6# Welcoming message for the assistant7welcome_message = "Hello, I'm Codecat, your Python coding assistant. How can I help you today?"8 9def respond(10 message,11 history: list[tuple[str, str]],12 system_message,13 max_tokens,14 temperature,15 top_p,16):17 # Check for empty or whitespace-only input18 if not message.strip():19 return "I'm only here to answer Python-related questions. Please ask something about Python."20 21 # Create messages for the model22 # Use only the system message for the first response23 messages = [{"role": "system", "content": f"{system_message}\n{welcome_message}"}] if len(history) == 0 else []24 25 for val in history:26 if val[0]:27 messages.append({"role": "user", "content": val[0]})28 if val[1]:29 messages.append({"role": "assistant", "content": val[1]})30 31 messages.append({"role": "user", "content": message})32 33 response = ""34 35 try:36 for message in client.chat_completion(37 messages,38 max_tokens=max_tokens,39 stream=True,40 temperature=temperature,41 top_p=top_p,42 ):43 token = message.choices[0].delta.content44 response += token45 yield response46 47 except Exception as e:48 return "I'm sorry, there seems to be an error with your input. Please check the syntax and try again."49 50 # Fallback for unrecognized input51 if not response:52 return "I'm not sure I understand. Can you please clarify your question?"53 54demo = gr.ChatInterface(55 respond,56 additional_inputs=[57 gr.Textbox(value="You are a friendly Chatbot.", label="System message"),58 gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),59 gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),60 gr.Slider(61 minimum=0.1,62 maximum=1.0,63 value=0.95,64 step=0.05,65 label="Top-p (nucleus sampling)",66 ),67 ],68)69 70if __name__ == "__main__":71 demo.launch()72 73 