import gradio as gr import requests import os ACCESS_TOKEN = os.getenv("HF_TOKEN") API_URL = "https://api-inference.huggingface.co/v1/chat/completions" HEADERS = {"Authorization": f"Bearer {ACCESS_TOKEN}"} def respond( message, history, system_message, max_tokens, temperature, top_p, frequency_penalty, seed, custom_model ): if seed == -1: seed = None messages = [{"role": "system", "content": system_message}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) model_to_use = custom_model.strip() if custom_model.strip() else "meta-llama/Llama-3.2-3B-Instruct" payload = { "model": model_to_use, "messages": messages, "max_tokens": max_tokens, "temperature": temperature, "top_p": top_p, "frequency_penalty": frequency_penalty, "seed": seed, } response = requests.post(API_URL, headers=HEADERS, json=payload) if response.status_code == 200: return response.json()["choices"][0]["message"]["content"] else: return "Error: " + response.text chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="ChatGPT is initializing...", likeable=True, layout="panel") system_message_box = gr.Textbox(value="System message here", label="System Message") max_tokens_slider = gr.Slider(1024, 2048, value=1024, step=100, label="Max Tokens") temperature_slider = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature") top_p_slider = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-P") frequency_penalty_slider = gr.Slider(-2.0, 2.0, value=0.0, step=0.1, label="Frequency Penalty") seed_slider = gr.Slider(-1, 65535, value=-1, step=1, label="Seed (-1 for random)") custom_model_box = gr.Textbox(value="meta-llama/Llama-3.2-3B-Instruct", label="Custom Model") voice_input = gr.Microphone(label="Speak your message") with gr.Blocks() as demo: gr.Markdown("# AI Chatbot with Voice Input") chatbot.render() with gr.Row(): system_message_box.render() max_tokens_slider.render() temperature_slider.render() top_p_slider.render() with gr.Row(): frequency_penalty_slider.render() seed_slider.render() custom_model_box.render() voice_input.render() voice_input.change(respond, inputs=[voice_input, chatbot, system_message_box, max_tokens_slider, temperature_slider, top_p_slider, frequency_penalty_slider, seed_slider, custom_model_box], outputs=chatbot) if __name__ == "__main__": demo.launch()