# 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"