File size: 3,641 Bytes
b7f426b
7e42f7f
b7f426b
a2dcfac
7e42f7f
a2dcfac
016c388
 
725f763
7e42f7f
a2dcfac
 
 
 
016c388
 
 
042d9eb
016c388
 
 
 
a2dcfac
016c388
 
 
 
 
 
b7f426b
016c388
 
 
 
 
 
 
 
725f763
 
 
 
 
 
 
 
016c388
 
042d9eb
 
 
a2dcfac
 
 
 
 
 
 
b7f426b
a2dcfac
 
 
725f763
a2dcfac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7f426b
a2dcfac
 
 
 
 
b7f426b
 
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
import os
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import pipeline

# === Inference Clients ===
llama_client = InferenceClient(provider="sambanova", api_key=os.environ["HF_TOKEN"])
minimax_client = InferenceClient(provider="novita", api_key=os.environ["HF_TOKEN"])
mistral_client = InferenceClient(provider="together", api_key=os.environ["HF_TOKEN"])

# === ChartGPT pipeline ===
chart_pipe = pipeline("text2text-generation", model="yuan-tian/chartgpt-llama3")

# === Chat Handler ===
def chat_with_model(model_choice, prompt, image_url):
    if not prompt:
        return "Please enter a text prompt."

    try:
        if model_choice == "LLaMA 4 (SambaNova)":
            content = [{"type": "text", "text": prompt}]
            if image_url:
                content.append({"type": "image_url", "image_url": {"url": image_url}})
            messages = [{"role": "user", "content": content}]
            completion = llama_client.chat.completions.create(
                model="meta-llama/Llama-4-Maverick-17B-128E-Instruct",
                messages=messages
            )
            return completion.choices[0].message.content

        elif model_choice == "MiniMax M1 (Novita)":
            messages = [{"role": "user", "content": prompt}]
            completion = minimax_client.chat.completions.create(
                model="MiniMaxAI/MiniMax-M1-80k",
                messages=messages
            )
            return completion.choices[0].message.content

        elif model_choice == "Mistral Mixtral-8x7B (Together)":
            messages = [{"role": "user", "content": prompt}]
            completion = mistral_client.chat.completions.create(
                model="mistralai/Mixtral-8x7B-Instruct-v0.1",
                messages=messages
            )
            return completion.choices[0].message.content

        else:
            return "Unsupported model selected."
    except Exception as e:
        return f"Error: {e}"

# === ChartGPT Handler ===
def generate_chart_code(prompt):
    try:
        response = chart_pipe(prompt, max_new_tokens=512)[0]["generated_text"]
        return response
    except Exception as e:
        return f"ChartGPT error: {e}"

# === Gradio UI ===
with gr.Blocks() as demo:
    gr.Markdown("## πŸ”₯ Multi-Tool AI Space: Chat + Chart Generator")

    with gr.Tabs():
        with gr.Tab("πŸ’¬ Multimodel Chat"):
            model_dropdown = gr.Dropdown(
                choices=[
                    "LLaMA 4 (SambaNova)",
                    "MiniMax M1 (Novita)",
                    "Mistral Mixtral-8x7B (Together)"
                ],
                value="LLaMA 4 (SambaNova)",
                label="Select Model"
            )
            prompt_input = gr.Textbox(label="Text Prompt", placeholder="Ask something...", lines=2)
            image_url_input = gr.Textbox(label="Optional Image URL (for LLaMA only)", placeholder="https://example.com/image.jpg")
            submit_btn = gr.Button("Generate Response")
            output_box = gr.Textbox(label="Response", lines=8)
            submit_btn.click(chat_with_model, [model_dropdown, prompt_input, image_url_input], output_box)

        with gr.Tab("πŸ“Š Chart Generator (ChartGPT)"):
            chart_prompt = gr.Textbox(label="Enter data analysis prompt", placeholder="Generate a bar chart comparing 2023 sales in North America and Europe", lines=3)
            chart_btn = gr.Button("Generate Chart Code")
            chart_output = gr.Textbox(label="ChartGPT Code Output", lines=16)
            chart_btn.click(generate_chart_code, chart_prompt, chart_output)

demo.launch()