Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
api_key = os.environ.get('MY_API_KEY')
|
6 |
+
|
7 |
+
def get_significant_events(year):
|
8 |
+
# Define the API endpoint
|
9 |
+
api_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/347fa3f3-d675-432c-b844-669ef8ee53df"
|
10 |
+
headers = {
|
11 |
+
'Authorization': f'Bearer {api_key}',
|
12 |
+
'Content-Type': 'application/json'
|
13 |
+
}
|
14 |
+
|
15 |
+
# Construct the instruction in Chinese
|
16 |
+
instruction = f"{year}年发生的重大事件"
|
17 |
+
|
18 |
+
data = {
|
19 |
+
"messages": [
|
20 |
+
{"content": instruction, "role": "user"}
|
21 |
+
],
|
22 |
+
"stream": False
|
23 |
+
}
|
24 |
+
|
25 |
+
# Make the API request
|
26 |
+
response = requests.post(api_url, headers=headers, json=data)
|
27 |
+
|
28 |
+
# Check the response status
|
29 |
+
if response.status_code == 200:
|
30 |
+
response_data = response.json()
|
31 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
32 |
+
content = response_data['choices'][0].get('message', {}).get('content', '')
|
33 |
+
return content
|
34 |
+
else:
|
35 |
+
return "未找到相关信息"
|
36 |
+
else:
|
37 |
+
return f"错误: {response.text}"
|
38 |
+
|
39 |
+
# Create the Gradio interface with Chinese labels
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=get_significant_events,
|
42 |
+
inputs=gr.Textbox(label="年份", placeholder="输入年份..."),
|
43 |
+
outputs=gr.Textbox(label="重大事件")
|
44 |
+
)
|
45 |
+
|
46 |
+
# Launch the app
|
47 |
+
if __name__ == "__main__":
|
48 |
+
interface.launch()
|