Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
技术与健康
deep backend
提交
377855e3
D
deep backend
项目概览
技术与健康
/
deep backend
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deep backend
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
377855e3
编写于
4月 17, 2025
作者:
P
practicer2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Thu Apr 17 18:00:00 CST 2025 inscode
上级
0a575cc3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
1 deletion
+58
-1
main.py
main.py
+58
-1
未找到文件。
main.py
浏览文件 @
377855e3
print
(
'欢迎来到 InsCode'
)
import
requests
\ No newline at end of file
import
json
import
os
from
flask
import
Flask
,
request
,
jsonify
app
=
Flask
(
__name__
)
# 请替换为你的 DeepSeek API 地址和 API Key
API_URL
=
"https://api.deepseek.com/v1"
API_KEY
=
os
.
getenv
(
'DeepSeek_API_KEY'
)
def
call_deepseek
(
prompt
,
max_new_tokens
=
150
):
"""
调用 DeepSeek API 生成文本Error: name 'os' is not defined
:param prompt: 输入的提示文本
:param max_new_tokens: 生成的最大新 token 数
:return: 生成的文本
"""
headers
=
{
"Content-Type"
:
"application/json"
,
"Authorization"
:
f
"Bearer
{
API_KEY
}
"
}
data
=
{
"prompt"
:
prompt
,
"max_new_tokens"
:
max_new_tokens
}
try
:
response
=
requests
.
post
(
API_URL
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
response
.
raise_for_status
()
result
=
response
.
json
().
get
(
"generated_text"
)
return
result
except
requests
.
RequestException
as
e
:
print
(
f
"调用 DeepSeek API 时请求出错:
{
e
}
"
)
except
(
KeyError
,
json
.
JSONDecodeError
)
as
e
:
print
(
f
"解析 DeepSeek API 响应时出错:
{
e
}
"
)
return
None
@
app
.
route
(
'/generate'
,
methods
=
[
'POST'
])
def
generate_text
():
data
=
request
.
get_json
()
prompt
=
data
.
get
(
'prompt'
)
max_new_tokens
=
data
.
get
(
'max_new_tokens'
,
150
)
if
not
prompt
:
return
jsonify
({
"error"
:
"Prompt is required"
}),
400
response
=
call_deepseek
(
prompt
,
max_new_tokens
)
if
response
:
return
jsonify
({
"generated_text"
:
response
})
else
:
return
jsonify
({
"error"
:
"Failed to generate text"
}),
500
if
__name__
==
"__main__"
:
app
.
run
(
debug
=
True
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录