File size: 4,039 Bytes
28d0df3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
from huggingface_hub import InferenceClient

import os

CHAT_MODEL = "Qwen/Qwen2.5-7B-Instruct-1M:featherless-ai"

HF_API_KEY = os.getenv("HF_API_KEY")

# ── Tab 1: Get weather  ────────────────────────────────────────────────────────────
def get_weather(location: str) -> str:
    """
    Retrieves the current weather for a given location.
    Args:
        location: The city or place for which to fetch the weather.
    """
    # In a real tool, call a weather API here.
    return f"The weather in {location} is sunny with mild temperatures."

tab1 = gr.Interface(
    fn=get_weather,
    inputs=[
        gr.Textbox(label="Location", placeholder="e.g. Brussels"),
    ],
    outputs=gr.Textbox(label="Weather"),
    title="Get Weather",
    examples=[["Brussels"], ["London"]],
)

# ── Tab 2: Ask an LLM ─────────────────────────────────────────────────────────
llm_client = InferenceClient(model=CHAT_MODEL, api_key=HF_API_KEY)

def ask_llm(prompt, system_prompt, max_tokens):
    response = llm_client.chat.completions.create(
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": prompt},
        ],
        max_tokens=int(max_tokens),
    )
    return response.choices[0].message.content

tab2 = gr.Interface(
    fn=ask_llm,
    inputs=[
        gr.Textbox(label="Your question", lines=3),
        gr.Textbox(label="System prompt", value="You are a helpful assistant.", lines=2),
        gr.Slider(50, 500, value=200, step=50, label="Max tokens"),
    ],
    outputs=gr.Textbox(label="Answer", lines=8),
    title="Ask an LLM",
)

# ── Tab 3: Image Generator ────────────────────────────────────────────────────
img_client = InferenceClient("black-forest-labs/FLUX.1-schnell", api_key=HF_API_KEY)

def generate_image(prompt, style):
    """
    Generate an image from a text description using Imagen via Google GenAI.
    Parameters
    ----------
    prompt : str
        A natural-language description of the image to generate.
    Returns
    -------
    PIL.Image.Image
        The generated image as a Pillow Image object, ready to display in Gradio.
    """

    full_prompt = f"{prompt}, {style} style" if style else prompt
    return img_client.text_to_image(full_prompt)

tab3 = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Describe your image", placeholder="A futuristic city at sunset...", lines=2),
        gr.Radio(
            ["photorealistic", "watercolor painting", "pixel art", "pencil sketch"],
            label="Style",
            value="photorealistic",
        ),
    ],
    outputs=gr.Image(label="Generated image"),
    title="Image Generator",
)

# ── Tab 4: Multi-turn Chat ────────────────────────────────────────────────────
chat_client = InferenceClient(model=CHAT_MODEL, api_key=HF_API_KEY)

def chat_fn(message, history):
    messages = history + [{"role": "user", "content": message}]
    response = chat_client.chat.completions.create(messages=messages)
    return response.choices[0].message.content

tab4 = gr.ChatInterface(
    fn=chat_fn,
    title="Chat with Qwen",
    examples=["Tell me a joke", "Explain AI in simple terms", "What is Gradio?"],
)

# ── Assemble ──────────────────────────────────────────────────────────────────
app = gr.TabbedInterface(
    [tab1, tab2, tab3, tab4],
    tab_names=["Get Weather", "Ask LLM", "Image Gen", "Chat"],
)
app.launch(mcp_server=True)