File size: 5,925 Bytes
c8190fb
 
 
ddb6847
c8190fb
 
ddb6847
 
 
c8190fb
 
 
ddb6847
 
c8190fb
ddb6847
c8190fb
ddb6847
 
 
 
c8190fb
 
 
 
 
ddb6847
777e963
c8190fb
 
 
ddb6847
 
c8190fb
 
 
 
 
ddb6847
 
 
c8190fb
 
 
 
 
ddb6847
c8190fb
 
ddb6847
 
c8190fb
ddb6847
c8190fb
 
 
777e963
ddb6847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8190fb
 
ddb6847
c8190fb
 
 
 
ddb6847
 
c8190fb
 
 
 
 
 
 
ddb6847
c8190fb
 
ddb6847
c8190fb
ddb6847
 
c8190fb
 
 
 
 
 
 
 
 
927e513
c8190fb
 
 
 
ddb6847
c8190fb
 
777e963
 
ddb6847
777e963
c8190fb
 
 
 
 
 
 
 
 
ddb6847
c8190fb
 
 
 
ddb6847
c8190fb
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# app.py

import os
import logging
from http.client import HTTPMessage
from http import HTTPStatus
import gradio as gr
from dashscope import Generation, Role
from dashscope.api_entities.dashscope_response import DashScopeResponse
from typing import List, Optional, Tuple, Dict
from urllib.error import HTTPError

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Environment variable for API token
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
if not YOUR_API_TOKEN:
    logging.error("API token not found in environment variables.")
    exit(1)

dashscope.api_key = YOUR_API_TOKEN

History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]

default_system = 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.'

def clear_session() -> History:
    return '', []

def modify_system_session(system: str) -> Tuple[str, str, History]:
    if not system:
        system = default_system
    return system, system, []

def history_to_messages(history: History, system: str) -> Messages:
    messages = [{'role': Role.SYSTEM, 'content': system}]
    for user_msg, assistant_msg in history:
        messages.append({'role': Role.USER, 'content': user_msg})
        messages.append({'role': Role.ASSISTANT, 'content': assistant_msg})
    return messages

def messages_to_history(messages: Messages) -> Tuple[str, History]:
    assert messages[0]['role'] == Role.SYSTEM
    system = messages[0]['content']
    history = [(msg['content'], next_msg['content']) for msg, next_msg in zip(messages[1::2], messages[2::2])]
    return system, history

def model_chat(query: Optional[str], history: Optional[History], system: str, radio: str) -> Tuple[str, str, History]:
    if not query:
        query = ''
    if not history:
        history = []
    messages = history_to_messages(history, system)
    messages.append({'role': Role.USER, 'content': query})
    label_model = f"qwen2.5-coder-{radio.lower()}-instruct"
    try:
        gen = Generation.call(
            model=label_model,
            messages=messages,
            result_format='message',
            stream=True
        )
        for response in gen:
            if response.status_code == HTTPStatus.OK:
                role = response.output.choices[0].message.role
                content = response.output.choices[0].message.content
                system, history = messages_to_history(messages + [{'role': role, 'content': content}])
                yield '', history, system
            else:
                raise HTTPError(code=response.status_code, msg=f'Request ID: {response.request_id}, Status Code: {response.status_code}, Error Code: {response.code}, Error Message: {response.message}', hdrs=HTTPMessage(), url='http://example.com', fp=None)
    except HTTPError as e:
        logging.error(f"HTTP Error: {e}")
        yield '', history, system
    except Exception as e:
        logging.error(f"Unexpected error: {e}")
        yield '', history, system

def choose_radio(radio, system):
    mark_ = gr.Markdown(value=f"<center><font size=8>Qwen2.5-Coder-{radio}-instruct👾</center>")
    chatbot = gr.Chatbot(label=f'Qwen2.5-Coder-{radio.lower()}-instruct')
    if not system:
        system = default_system
    return mark_, chatbot, system, system, ""

def update_other_radios(value, other_radio1, other_radio2):
    if not value:
        if other_radio1:
            selected = other_radio1
        else:
            selected = other_radio2
        return selected, other_radio1, other_radio2
    return value, "", ""

def main():
    # Create two tabs
    with gr.Blocks() as demo:
        with gr.Row():
            options_coder = ["0.5B", "1.5B", "3B", "7B", "14B", "32B"]
            with gr.Row():
                radio = gr.Radio(choices=options_coder, label="Qwen2.5-Coder:", value="32B")

        with gr.Row():
            with gr.Accordion():
                mark_ = gr.Markdown("""<center><font size=8>Qwen2.5-Coder-32B-Instruct Bot👾</center>""")
                with gr.Row():
                    with gr.Column(scale=3):
                        system_input = gr.Textbox(value=default_system, lines=1, label='System')
                    with gr.Column(scale=1):
                        modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
                    system_state = gr.Textbox(value=default_system, visible=False)
                chatbot = gr.Chatbot(label='Qwen2.5-Coder-32B-Instruct')
                textbox = gr.Textbox(lines=1, label='Input')
                
                with gr.Row():
                    clear_history = gr.Button("🧹 Clear History")
                    submit = gr.Button("🚀 Send")
                
                textbox.submit(model_chat,
                               inputs=[textbox, chatbot, system_state, radio],
                               outputs=[textbox, chatbot, system_input])
                submit.click(model_chat,
                             inputs=[textbox, chatbot, system_state, radio],
                             outputs=[textbox, chatbot, system_input],
                             concurrency_limit=100)
                clear_history.click(fn=clear_session,
                                    inputs=[],
                                    outputs=[textbox, chatbot])
                modify_system.click(fn=modify_system_session,
                                    inputs=[system_input],
                                    outputs=[system_state, system_input, chatbot])
        
        radio.change(choose_radio,
                     inputs=[radio, system_input],
                     outputs=[mark_, chatbot, system_state, system_input, textbox])
    
    demo.queue(api_open=False, default_concurrency_limit=40)
    demo.launch(max_threads=5, server_name="0.0.0.0", server_port=7860, share=True)

if __name__ == "__main__":
    main()