File size: 3,426 Bytes
34a1cb8
 
 
 
 
 
 
d748c8c
34a1cb8
 
 
d748c8c
34a1cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff64e0
34a1cb8
 
 
fff64e0
 
 
 
 
 
 
 
 
34a1cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef042dd
262dde0
34a1cb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620aca7
34a1cb8
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
import gradio as gr
from collections.abc import Generator
from openai import OpenAI
# from gradio.chat_interface import ChatInterface
from chat_interface import ChatInterface

USERNAME = "ahmedheakl"
SPACE_NAME = "AIN-Arabic-VLM"
TITLE = "AIN Arabic VLM"
DESCRIPTION = "Welcome to the AIN Arabic VLM chatbot. The best Arabic-English VLM developed by MBZUAI."
TOP_N_HISTORY = 2
LOGO_PATH = "https://huggingface.co./spaces/ahmedheakl/AIN-Arabic-VLM/resolve/main/logo.jpeg"



def load_chat(
    base_url: str,
    model: str,
    token: str | None = None,
    *,
    system_message: str | None = None,
    **kwargs,
) -> gr.ChatInterface:
    client = OpenAI(api_key=token, base_url=base_url)
    start_message = (
        [{"role": "system", "content": system_message}] if system_message else []
    )

    def open_api_stream(
        message: str, history: list | None
    ) -> Generator[str, None, None]:
        history = history or start_message
        print(history)
        if len(history) > 0 and isinstance(history[0], (list, tuple)):
            history = history[:TOP_N_HISTORY]
            history = ChatInterface._tuples_to_messages(history)
        elif len(history) > 0 and isinstance(history[0], dict):
            hist = []
            for i, h in enumerate(history):
                content = h.get("content", "")
                if isinstance(content, tuple):
                    hist.append({"role": "user", "content": [{"type": "image_url", "image_url": {"url": content[0]}}]})
                else:
                    hist.append({"role": "user", "content": [{"type": "text", "text": content}]})
            history = hist
        files = message.get('files', [])
        content = [
            {"type": "text", "text": message.get('text', '')}
        ]
        if files:
            src_path = files[0]
            image_url = f"https://{USERNAME}-{SPACE_NAME}.hf.space/gradio_api/file={src_path}"
            content.append({"type": "image_url", "image_url": {"url": image_url}})
        stream = client.chat.completions.create(
            model=model,
            messages=history + [{"role": "user", "content": content}],
            stream=True,
        )
        response = ""
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                response += chunk.choices[0].delta.content
                yield response

    return gr.ChatInterface(
        open_api_stream, type="messages", **kwargs, 
    )

load_chat(
    "https://0f21-5-195-0-150.ngrok-free.app/v1",
    model="test",
    token="ollama",
    multimodal=True,
    title=TITLE,
    description=DESCRIPTION,
    theme="ocean",
    examples=[
        {
            "text": "ุฃุฎุจุฑู†ูŠ ู…ุง ุงุณู… ุงู„ู…ุจู†ู‰ ุงู„ู…ูˆุฌูˆุฏ ููŠ ุงู„ุตูˆุฑุฉ ูˆุงู„ุดูŠุก ุงู„ู…ุซูŠุฑ ู„ู„ุงู‡ุชู…ุงู… ููŠู‡",
            "files": ["https://cdn.mos.cms.futurecdn.net/5HrnHp9ybAqYrtruKAsfkN-1200-80.jpg"],
        },
        {
            "text": "ู…ุง ู‡ูˆ ุงู„ุนู„ู… ุงู„ู…ูˆุฌูˆุฏ ููŠ ุงู„ุตูˆุฑุฉุŸ", 
            "files": ["https://mtc.ae/wp-content/uploads/2023/09/Satin-UAE-Flag-UAE-F-B-Blank.jpg"],
        },
        {
            "text": "How many people are there in the image?",
            "files": ["https://i0.wp.com/eatpitapita.com/wp-content/uploads/2020/02/Arab-Muslim-or-Middle-Eastern-Preview.jpg"]
        },
    ],
    cache_examples=False
).queue().launch(allowed_paths=["/static"])