import os os.system('pip install minijinja') import gradio as gr from huggingface_hub import InferenceClient import torch import spaces # Initialize the client with your model client = InferenceClient("karpathy/gpt2_1558M_final2_hf") # Replace with your model's name or endpoint default_system = 'You are a helpful assistant' @spaces.GPU def respond(message, history, system_message, max_tokens, temperature, top_p): messages = [{"role": "system", "content": system_message}] for user, assistant in history: messages.append({"role": "user", "content": user}) messages.append({"role": "assistant", "content": assistant}) messages.append({"role": "user", "content": message}) response = "" for chunk in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): if chunk.choices[0].delta.content is not None: token = chunk.choices[0].delta.content response += token yield history + [(message, response)] # If the response is empty, yield a default message if not response: yield history + [(message, "I apologize, but I couldn't generate a response.")] def clear_session(): return "", [] def modify_system_session(system): if not system: system = default_system return system, system, [] def use_example(example): return example def set_unicorn_example(): return unicorn_example def set_time_travel_example(): return time_travel_example # Define example prompts unicorn_example = "In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English." time_travel_example = "Explain the grandfather paradox in time travel and propose a potential resolution." with gr.Blocks() as demo: gr.Markdown("