import os import json from datetime import datetime import gradio as gr from openai import OpenAI def print_now(msg): now = datetime.now() formatted_time = now.strftime("%Y-%m-%d %H:%M:%S.%f") print(f"{msg}:{formatted_time}") return formatted_time def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): try: default_system ="You are Tencent's helpful AI assistant Hunyuan." messages = [{"Role": "system", "Content": default_system}] client = OpenAI( api_key=os.getenv('HUNYUAN_API_KEY'), base_url="https://api.hunyuan.cloud.tencent.com/v1", ) for val in history: if val[0] and val[1]: messages.append({"Role": "user", "Content": val[0]}) messages.append({"Role": "assistant", "Content": val[1]}) messages.append({"Role": "user", "Content": message}) completion = client.chat.completions.create( model="hunyuan-t1-latest", messages=messages, stream=True, extra_body={ "stream_moderation": True, "enable_enhancement": False, } ) response = "" is_reasoning_start = True is_reasoning_end = True for event in completion: if hasattr(event.choices[0].delta, 'reasoning_content'): if is_reasoning_start: response += '> **开始思考**\n\n' is_reasoning_start = False token = event.choices[0].delta.reasoning_content# Wrap reasoning_content in a span with a lighter color response += f'{token}' else: if is_reasoning_end: response += '> **结束思考**\n\n' is_reasoning_end = False token = event.choices[0].delta.content# Wrap content in a span with a normal color response += f'{token}' yield response except Exception as e: raise gr.Error(f"发生错误: {str(e)}") example_prompts = [ ["Write a short papragraph where the 1st letter of each sentence spells out the word 'CODE'. The message should appear natural and not obviously hide this pattern."], ["Compose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions."], ["Why has online learning been able to spread rapidly in recent years?"], ["How many 'e' in Deeplearning?"], ["Write a 3-line poem"] ] latex_delimiters = [ {"left": "$$", "right": "$$", "display": True}, {"left": "\\[", "right": "\\]", "display": True},{"left": "$", "right": "$", "display": False}, {"left": "\\(", "right": "\\)", "display": False} ] chatbot = gr.Chatbot(latex_delimiters=latex_delimiters, scale=9) demo = gr.ChatInterface(respond, title="Hunyuan T1", examples=example_prompts, chatbot=chatbot ) if __name__ == "__main__": demo.queue(default_concurrency_limit=40) demo.launch(max_threads=40)