返回提交历史
Modified
README.md
+25
-1
Modified
g4f/Provider/needs_auth/Gemini.py
+21
-18
Modified
g4f/__init__.py
+1
-1
XFEstudio/gpt4free
Add example for Image Upload & Generation
4b41a8f4
代码差异
3 个文件
+47
-20
@@ -316,7 +316,7 @@ For generating images with Bing and for the OpenAi Chat you need cookies or a t
316
316
```python
317
317
from g4f import set_cookies
318
318
319
set_cookies(".bing", {
319
set_cookies(".bing.com", {
320
320
"_U": "cookie value"
321
321
})
322
322
set_cookies("chat.openai.com", {
@@ -336,6 +336,30 @@ pip install browser_cookie3
336
336
pip install g4f[webdriver]
337
337
```
338
338
339
##### Image Upload & Generation
340
341
Image upload and generation are supported by three main providers:
342
343
- **Bing & Other GPT-4 Providers:** Utilizes Microsoft's Image Creator.
344
- **Google Gemini:** Available for free accounts with IP addresses outside Europe.
345
- **OpenaiChat with GPT-4:** Accessible for users with a Plus subscription.
346
347
```python
348
import g4f
349
350
# Setting up the request for image creation
351
response = g4f.ChatCompletion.create(
352
model=g4f.models.default, # Using the default model
353
provider=g4f.Provider.Gemini, # Specifying the provider as Gemini
354
messages=[{"role": "user", "content": "Create an image like this"}],
355
image=open("images/g4f.png", "rb"), # Image input can be a data URI, bytes, PIL Image, or IO object
356
image_name="g4f.png" # Optional: specifying the filename
357
)
358
359
# Displaying the response
360
print(response)
361
```
362
339
363
##### Using Browser
340
364
341
365
Some providers using a browser to bypass the bot protection. They using the selenium webdriver to control the browser. The browser settings and the login data are saved in a custom directory. If the headless mode is enabled, the browser windows are loaded invisibly. For performance reasons, it is recommended to reuse the browser instances and close them yourself at the end:
@@ -65,25 +65,28 @@ class Gemini(AsyncGeneratorProvider):
65
65
) -> AsyncResult:
66
66
prompt = format_prompt(messages)
67
67
68
try:
69
driver = get_browser(proxy=proxy)
68
if not cookies:
69
driver = None
70
70
try:
71
driver.get(f"{cls.url}/app")
72
WebDriverWait(driver, 5).until(
73
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
74
)
75
except:
76
login_url = os.environ.get("G4F_LOGIN_URL")
77
if login_url:
78
yield f"Please login: [Google Gemini]({login_url})\n\n"
79
WebDriverWait(driver, 240).until(
80
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
81
)
82
cookies = get_driver_cookies(driver)
83
except MissingRequirementsError:
84
pass
85
finally:
86
driver.close()
71
driver = get_browser(proxy=proxy)
72
try:
73
driver.get(f"{cls.url}/app")
74
WebDriverWait(driver, 5).until(
75
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
76
)
77
except:
78
login_url = os.environ.get("G4F_LOGIN_URL")
79
if login_url:
80
yield f"Please login: [Google Gemini]({login_url})\n\n"
81
WebDriverWait(driver, 240).until(
82
EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea"))
83
)
84
cookies = get_driver_cookies(driver)
85
except MissingRequirementsError:
86
pass
87
finally:
88
if driver:
89
driver.close()
87
90
88
91
if not cookies:
89
92
cookies = get_cookies(".google.com", False)
@@ -136,7 +136,7 @@ class ChatCompletion:
136
136
provider = patch_provider(provider)
137
137
138
138
result = provider.create_completion(model, messages, stream, **kwargs)
139
return result if stream else ''.join(result)
139
return result if stream else ''.join([str(chunk) for chunk in result])
140
140
141
141
@staticmethod
142
142
def create_async(model : Union[Model, str],