File size: 2,427 Bytes
b9a427c
 
e1daa55
b9a427c
25cd75a
e1daa55
b9a427c
 
 
 
 
 
e1daa55
 
 
 
b9a427c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1daa55
 
 
 
 
 
 
 
 
e2e2130
 
 
 
 
b9a427c
 
 
e1daa55
 
 
 
 
 
 
 
 
e2e2130
b9a427c
 
 
 
e1daa55
b9a427c
 
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
import os

import gradio as gr
import requests
from models import MODEL_IDS

# Modal endpoint URL - set this after deploying backend.py
MODAL_ENDPOINT = os.environ.get("MODAL_ENDPOINT", "")
# API key for authenticating with Modal backend
MODEL_SITE_API_KEY = os.environ.get("MODEL_SITE_API_KEY", "")
# Password for Gradio login (any username accepted)
SITE_PASSWORD = os.environ.get("SITE_PASSWORD", "")


def make_respond_fn(model_id: str):
    def respond(message: str, history: list[tuple[str, str]]) -> str:
        if not MODAL_ENDPOINT:
            return "Error: MODAL_ENDPOINT environment variable not set"

        try:
            response = requests.post(
                MODAL_ENDPOINT,
                headers={"X-API-Key": MODEL_SITE_API_KEY},
                json={
                    "model_id": model_id,
                    "message": message,
                    "history": history,
                },
                timeout=120,  # Cold start can take a while
            )
            response.raise_for_status()
            data = response.json()

            if "error" in data:
                return f"Error: {data['error']}"

            return data.get("response", "No response received")

        except requests.exceptions.Timeout:
            return "Error: Request timed out. The model may be starting up, please try again."
        except requests.exceptions.RequestException as e:
            return f"Error: {e}"

    return respond


# Create tabbed interface with one chat per model
with gr.Blocks(title="posttraining-practice") as demo:
    gr.Markdown("# posttraining-practice")
    gr.Markdown("Chat with different fine-tuned models")

    missing = [
        v
        for v in ["MODAL_ENDPOINT", "MODEL_SITE_API_KEY", "SITE_PASSWORD"]
        if not os.environ.get(v)
    ]
    if missing:
        gr.Markdown(f"⚠️ **Warning:** Missing secrets: {', '.join(missing)}")

    with gr.Tabs():
        for model_id in MODEL_IDS:
            short_name = model_id.split("/")[-1]
            with gr.Tab(short_name):
                gr.ChatInterface(
                    fn=make_respond_fn(model_id),
                    description=f"Chatting with: {model_id}",
                )


def check_password(username: str, password: str) -> bool:
    return password == SITE_PASSWORD


if __name__ == "__main__":
    auth = check_password if SITE_PASSWORD else None
    demo.launch(auth=auth)