import gradio as gr from huggingface_hub import InferenceClient # Initialize Mistral client client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") # Prompt formatting for Mistral def format_prompt(message, history): prompt = "" for user_prompt, bot_response in history: prompt += f"[INST] {user_prompt} [/INST] {bot_response} " prompt += f"[INST] {message} [/INST]" return prompt # Generation function with streaming def generate(prompt, history, temperature, max_new_tokens, top_p, repetition_penalty): formatted_prompt = format_prompt(prompt, history) stream = client.text_generation( formatted_prompt, temperature=float(temperature), max_new_tokens=int(max_new_tokens), top_p=float(top_p), repetition_penalty=float(repetition_penalty), do_sample=True, seed=42, stream=True, return_full_text=False, ) output = "" for response in stream: output += response.token.text yield output # Build unified mobile-friendly interface with gr.Blocks(theme="soft") as demo: gr.Markdown("### 📱 Multi‑Model Playground (Mobile Optimized)") # Tab 1: Mistral Chat with gr.Tab("Mistral Chat"): gr.ChatInterface( fn=generate, chatbot=gr.Chatbot(layout="bubble", show_label=False), additional_inputs=[ gr.Dropdown([0.7, 0.9, 1.0], value=0.9, label="Temperature"), gr.Dropdown([128, 256, 512], value=256, label="Max new tokens"), gr.Dropdown([0.8, 0.9, 1.0], value=0.9, label="Top‑p"), gr.Dropdown([1.0, 1.2, 1.5], value=1.2, label="Repetition penalty"), ], ) # Tab 2: DALL·E 3 XL with gr.Tab("DALL·E 3 XL"): gr.load("models/ehristoforu/dalle-3-xl-v2", src="spaces") # Tab 3: Phi‑3 Mini with gr.Tab("Phi‑3 Mini"): gr.load("models/microsoft/Phi-3-mini-4k-instruct", src="spaces") # Launch with share=True for mobile browser access demo.launch(share=True)