import os import requests import gradio as gr api_key = os.environ.get('MY_API_KEY') def get_significant_events(year): # Define the API endpoint api_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/347fa3f3-d675-432c-b844-669ef8ee53df" headers = { 'Authorization': f'Bearer {api_key}', 'Content-Type': 'application/json' } # Construct the instruction in Chinese instruction = f"列出{year}年发生的主要事件" data = { "messages": [ {"content": instruction, "role": "user"} ], "stream": False } # Make the API request response = requests.post(api_url, headers=headers, json=data) # Check the response status if response.status_code == 200: response_data = response.json() if 'choices' in response_data and len(response_data['choices']) > 0: content = response_data['choices'][0].get('message', {}).get('content', '') return content else: return "未找到相关信息" else: return f"错误: {response.text}" # Create the Gradio interface with Chinese labels interface = gr.Interface( fn=get_significant_events, inputs=gr.Textbox(label="年份", placeholder="输入年份..."), outputs=gr.Textbox(label="重大事件") ) # Launch the app if __name__ == "__main__": interface.launch()