返回提交历史
Deleted
new.py
+0
-215
XFEstudio/gpt4free
Fix build docker image
ecbf86c2
代码差异
1 个文件
+0
-215
@@ -1,215 +0,0 @@
1
import json
2
import uuid
3
import g4f.debug
4
import requests
5
from g4f.client import Client
6
7
def upload_and_process(files_or_urls, bucket_id=None):
8
if bucket_id is None:
9
bucket_id = str(uuid.uuid4())
10
11
if isinstance(files_or_urls, list): # URLs
12
files = {'files': ('downloads.json', json.dumps(
13
files_or_urls), 'application/json')}
14
elif isinstance(files_or_urls, dict): # Files
15
files = files_or_urls
16
else:
17
raise ValueError(
18
"files_or_urls must be a list of URLs or a dictionary of files")
19
20
upload_response = requests.post(
21
f'http://localhost:8080/backend-api/v2/files/{bucket_id}', files=files)
22
23
if upload_response.status_code == 200:
24
upload_data = upload_response.json()
25
print(f"Upload successful. Bucket ID: {upload_data['bucket_id']}")
26
else:
27
print(
28
f"Upload failed: {upload_response.status_code} - {upload_response.text}")
29
30
response = requests.get(
31
f'http://localhost:8080/backend-api/v2/files/{bucket_id}', stream=True, headers={'Accept': 'text/event-stream'})
32
for line in response.iter_lines():
33
if line:
34
line = line.decode('utf-8')
35
if line.startswith('data:'):
36
try:
37
data = json.loads(line[5:]) # remove data: prefix
38
if "action" in data:
39
print(f"SSE Event: {data}")
40
elif "error" in data:
41
print(f"Error: {data['error']['message']}")
42
else:
43
# Assuming it's file content
44
print(f"File data received: {data}")
45
except json.JSONDecodeError as e:
46
print(f"Error decoding JSON: {e}")
47
else:
48
print(f"Unhandled SSE event: {line}")
49
response.close()
50
return bucket_id
51
52
53
# Example with URLs
54
55
# Enable debug mode
56
g4f.debug.logging = True
57
58
client = Client()
59
60
# Upload example file
61
files = {'files': ('demo.docx', open('demo.docx', 'rb'))}
62
bucket_id = upload_and_process(files)
63
64
# Send request with file:
65
response = client.chat.completions.create(
66
model='gpt-4o',
67
messages=[{"role": "user", "content": [
68
{"type": "text", "text": "Discribe this file."},
69
{"bucket_id": bucket_id}
70
]}],
71
)
72
print(response.choices[0].message.content)
73
exit()
74
# import asyncio
75
# from g4f.client import AsyncClient
76
# import g4f.Provider
77
78
# async def main():
79
# client = AsyncClient(provider=g4f.Provider.MarkItDown)
80
81
# # Transcribe a audio file
82
# with open("audio.wav", "rb") as audio_file:
83
# response = await client.chat.completions.create("", media=[audio_file])
84
# print(response.choices[0].message.content)
85
86
# if __name__ == "__main__":
87
# asyncio.run(main())
88
89
#exit()
90
import requests
91
92
# Open the audio file in binary mode
93
with open('demo.docx', 'rb') as audio_file:
94
# Make the POST request
95
response = requests.post('http://localhost:8080/api/markitdown', files={'file': audio_file})
96
97
# Check the response and print the transcription
98
if response.status_code == 200:
99
data = response.json()
100
print(data['text'])
101
else:
102
print(f"Error: {response.status_code}, {response.text}")
103
exit()
104
105
# from openai import OpenAI
106
# client = OpenAI(base_url="http://localhost:8080/v1", api_key="secret")
107
108
# with open("audio.wav", "rb") as file:
109
# transcript = client.audio.transcriptions.create(
110
# model="",
111
# extra_body={"provider": "MarkItDown"},
112
# file=file
113
# )
114
# print(transcript.text)
115
116
exit()
117
import asyncio
118
import time
119
from g4f import AsyncClient
120
from g4f.Provider import PollinationsAI
121
async def test():
122
client = AsyncClient()
123
response = client.chat.completions.create("guten tag", stream=True, provider=HarProvider)
124
async for chunk in response:
125
if chunk.choices[0].finish_reason == "stop":
126
break
127
print(chunk.choices[0].delta.content, end="", flush=True)
128
print()
129
130
asyncio.run(test())
131
time.sleep(1)
132
exit()
133
134
client = Client(provider=PollinationsAI)
135
response = client.media.generate("Hello", model="hypnosis-tracy")
136
response.data[0].save("hypnosis.mp3")
137
138
client = Client(provider=Gemini)
139
response = client.media.generate("Hello", model="gemini-audio")
140
response.data[0].save("gemini.ogx")
141
142
client = Client(provider=EdgeTTS)
143
response = client.media.generate("Hello", audio={"locale": "en-US"})
144
response.data[0].save("edge-tts.mp3")
145
146
exit()
147
import requests
148
import uuid
149
import json
150
151
def upload_and_process(files_or_urls, bucket_id=None):
152
if bucket_id is None:
153
bucket_id = str(uuid.uuid4())
154
155
if isinstance(files_or_urls, list): #URLs
156
files = {'files': ('downloads.json', json.dumps(files_or_urls), 'application/json')}
157
elif isinstance(files_or_urls, dict): #Files
158
files = files_or_urls
159
else:
160
raise ValueError("files_or_urls must be a list of URLs or a dictionary of files")
161
162
upload_response = requests.post(f'http://localhost:8080/v1/files/{bucket_id}', files=files)
163
164
if upload_response.status_code == 200:
165
upload_data = upload_response.json()
166
print(f"Upload successful. Bucket ID: {upload_data['bucket_id']}")
167
else:
168
print(f"Upload failed: {upload_response.status_code} - {upload_response.text}")
169
170
response = requests.get(f'http://localhost:8080/v1/files/{bucket_id}', stream=True, headers={'Accept': 'text/event-stream'})
171
for line in response.iter_lines():
172
if line:
173
line = line.decode('utf-8')
174
if line.startswith('data:'):
175
try:
176
data = json.loads(line[5:]) #remove data: prefix
177
if "action" in data:
178
print(f"SSE Event: {data}")
179
elif "error" in data:
180
print(f"Error: {data['error']['message']}")
181
else:
182
print(f"File data received: {data}") #Assuming it's file content
183
except json.JSONDecodeError as e:
184
print(f"Error decoding JSON: {e}")
185
else:
186
print(f"Unhandled SSE event: {line}")
187
response.close()
188
return bucket_id
189
190
# Example with URLs
191
#Example with files
192
#files = {'files': open('document.pdf', 'rb'), 'files': open('data.json', 'rb')}
193
#bucket_id = upload_and_process(files)
194
import asyncio
195
from g4f.client import Client
196
197
import g4f.debug
198
g4f.debug.logging = True
199
200
client = Client()
201
202
203
files = {'files': ('demo.docx', open('demo.docx', 'rb'))}
204
bucket_id = upload_and_process(files)
205
206
207
response = client.chat.completions.create(
208
[{"role": "user", "content": [
209
{"type": "text", "text": "Discribe this file."},
210
{"bucket_id": bucket_id}
211
]}],
212
"o1",
213
)
214
print(response.choices[0].message.content)
215