Spaces:
Running
Running
dialogueeeeee
commited on
Commit
·
2e8ce10
1
Parent(s):
789886f
Add application file
Browse files- .env +1 -0
- app.py +25 -24
- requirements.txt +1 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPENAI_API_KEY="sk-ut4AJ8UzqsoTVaeIATrXBGfLRcYhZbba04jnSZyOwlT3BlbkFJsEa4UxwYuflmBYBi2oLQnZJIB8-zCzTWJ9QQLMzt8A"
|
app.py
CHANGED
@@ -3,13 +3,22 @@ from PIL import Image
|
|
3 |
from io import BytesIO
|
4 |
import requests
|
5 |
import time
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
class ImageProcessor:
|
15 |
def __init__(self, api_key):
|
@@ -60,7 +69,7 @@ class ImageProcessor:
|
|
60 |
task_id = self.submit_images(image_bytes_list)
|
61 |
return self.get_result(task_id)
|
62 |
|
63 |
-
# 设置API密钥
|
64 |
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
|
65 |
image_processor = ImageProcessor(api_key)
|
66 |
|
@@ -78,7 +87,7 @@ def process_input(text=None, images=None, audio=None):
|
|
78 |
# 使用ImageProcessor处理图像
|
79 |
image_bytes_list = []
|
80 |
for image in images:
|
81 |
-
img = Image.open(image)
|
82 |
image_bytes = BytesIO()
|
83 |
img.save(image_bytes, format="PNG")
|
84 |
image_bytes.seek(0)
|
@@ -86,7 +95,9 @@ def process_input(text=None, images=None, audio=None):
|
|
86 |
|
87 |
try:
|
88 |
processed_image_result = image_processor.process_images(image_bytes_list)
|
|
|
89 |
prompt += f"\n乐谱的内容如下,这是一首杜维诺伊的曲子,请你根据他的曲风回答问题: {processed_image_result}"
|
|
|
90 |
except Exception as e:
|
91 |
return f"Error processing image: {e}", None
|
92 |
|
@@ -96,21 +107,11 @@ def process_input(text=None, images=None, audio=None):
|
|
96 |
|
97 |
# 使用GPT API进行处理
|
98 |
try:
|
99 |
-
'''
|
100 |
-
from zhipuai import ZhipuAI
|
101 |
-
client = ZhipuAI(api_key="7cfa094b2c6867229659831340af9f2c.BIl47duC5fufSY8V") # 填写您自己的APIKey
|
102 |
-
response = client.chat.completions.create(
|
103 |
-
model="glm-4", # 填写需要调用的模型名称
|
104 |
-
messages=[
|
105 |
-
{"role": "user", "content": prompt},
|
106 |
-
],
|
107 |
-
)
|
108 |
-
'''
|
109 |
messages.append({"role": "user", "content": prompt})
|
110 |
response = client.chat.completions.create(
|
111 |
model=engine,
|
112 |
messages=messages,
|
113 |
-
temperature=0.
|
114 |
max_tokens=4096,
|
115 |
top_p=0.95,
|
116 |
frequency_penalty=0,
|
@@ -118,7 +119,7 @@ def process_input(text=None, images=None, audio=None):
|
|
118 |
stop=None
|
119 |
)
|
120 |
print(response.choices[0].message.content)
|
121 |
-
return response.choices[0].message.content
|
122 |
except Exception as e:
|
123 |
return f"Error: {e}", None
|
124 |
|
@@ -126,17 +127,17 @@ def process_input(text=None, images=None, audio=None):
|
|
126 |
iface = gr.Interface(
|
127 |
fn=process_input,
|
128 |
inputs=[
|
129 |
-
gr.Textbox(label="Input Text", placeholder="Enter text here"),
|
130 |
-
gr.File(label="Input Images", file_count="multiple", type="
|
131 |
# gr.Audio(label="Input Audio", type="filepath"),
|
|
|
132 |
],
|
133 |
outputs=[
|
134 |
gr.Textbox(label="Output Text"),
|
135 |
# gr.Audio(label="Output Audio") # 目前示例中未处理音频输出
|
136 |
],
|
137 |
-
live=
|
138 |
)
|
139 |
|
140 |
# 启动Gradio应用
|
141 |
iface.launch()
|
142 |
-
|
|
|
3 |
from io import BytesIO
|
4 |
import requests
|
5 |
import time
|
6 |
+
import openai
|
7 |
+
import os
|
8 |
+
from openai import OpenAI
|
9 |
+
from dotenv import load_dotenv
|
10 |
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
client = OpenAI(
|
14 |
+
# This is the default and can be omitted
|
15 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
#api_key=os.environ.get("OPENAI_API_KEY"),
|
17 |
+
)
|
18 |
+
|
19 |
+
# 设置OpenAI API密钥
|
20 |
+
openai.api_key = "sk-ut4AJ8UzqsoTVaeIATrXBGfLRcYhZbba04jnSZyOwlT3BlbkFJsEa4UxwYuflmBYBi2oLQnZJIB8-zCzTWJ9QQLMzt8A"
|
21 |
+
engine = "gpt-4o-mini"
|
22 |
|
23 |
class ImageProcessor:
|
24 |
def __init__(self, api_key):
|
|
|
69 |
task_id = self.submit_images(image_bytes_list)
|
70 |
return self.get_result(task_id)
|
71 |
|
72 |
+
# 设置Music API密钥
|
73 |
api_key = 'ddc85b14-bd83-4757-9bc4-8a11194da536'
|
74 |
image_processor = ImageProcessor(api_key)
|
75 |
|
|
|
87 |
# 使用ImageProcessor处理图像
|
88 |
image_bytes_list = []
|
89 |
for image in images:
|
90 |
+
img = Image.open(image.name)
|
91 |
image_bytes = BytesIO()
|
92 |
img.save(image_bytes, format="PNG")
|
93 |
image_bytes.seek(0)
|
|
|
95 |
|
96 |
try:
|
97 |
processed_image_result = image_processor.process_images(image_bytes_list)
|
98 |
+
#prompt += f"\n乐谱的内容如下,这是一首杜维诺伊的曲子,请你根据他的曲风回答问题: {processed_image_result}"
|
99 |
prompt += f"\n乐谱的内容如下,这是一首杜维诺伊的曲子,请你根据他的曲风回答问题: {processed_image_result}"
|
100 |
+
|
101 |
except Exception as e:
|
102 |
return f"Error processing image: {e}", None
|
103 |
|
|
|
107 |
|
108 |
# 使用GPT API进行处理
|
109 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
messages.append({"role": "user", "content": prompt})
|
111 |
response = client.chat.completions.create(
|
112 |
model=engine,
|
113 |
messages=messages,
|
114 |
+
temperature=0.2,
|
115 |
max_tokens=4096,
|
116 |
top_p=0.95,
|
117 |
frequency_penalty=0,
|
|
|
119 |
stop=None
|
120 |
)
|
121 |
print(response.choices[0].message.content)
|
122 |
+
return response.choices[0].message.content
|
123 |
except Exception as e:
|
124 |
return f"Error: {e}", None
|
125 |
|
|
|
127 |
iface = gr.Interface(
|
128 |
fn=process_input,
|
129 |
inputs=[
|
130 |
+
gr.Textbox(label="Input Text", placeholder="Enter text here", lines=2), # Text input with submit button
|
131 |
+
gr.File(label="Input Images", file_count="multiple", type="file"), # 支持多文件上传
|
132 |
# gr.Audio(label="Input Audio", type="filepath"),
|
133 |
+
|
134 |
],
|
135 |
outputs=[
|
136 |
gr.Textbox(label="Output Text"),
|
137 |
# gr.Audio(label="Output Audio") # 目前示例中未处理音频输出
|
138 |
],
|
139 |
+
live=False,
|
140 |
)
|
141 |
|
142 |
# 启动Gradio应用
|
143 |
iface.launch()
|
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
openai
|
2 |
gradio
|
3 |
zhipuai
|
|
|
|
1 |
openai
|
2 |
gradio
|
3 |
zhipuai
|
4 |
+
python-dotenv
|