Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

[Bug]: run EdgeGPT with flask #622

Open
1 task done
iervn6341-ovo opened this issue Jul 20, 2023 · 3 comments
Open
1 task done

[Bug]: run EdgeGPT with flask #622

iervn6341-ovo opened this issue Jul 20, 2023 · 3 comments

Comments

@iervn6341-ovo
Copy link

iervn6341-ovo commented Jul 20, 2023

Is there an existing issue for this?

  • I have searched the existing issues and checked the recent builds/commits

What happened?

My code:

class Edgegpt():
	async def main(self):
		cookies = json.loads(open("./bing_cookies_.json", encoding="utf-8").read())  # might omit cookies option
                bot = await Chatbot.create(cookies=cookies)
		response = await bot.ask(prompt="introduce your self.", conversation_style=ConversationStyle.creative, simplify_response=True)
		response = response['text'].encode('utf-8').decode('utf-8')
		print(response)
		
class bot():
	def __init__(self, api):
		self.gpt = Edgegpt()
	
	def main_bot(self):
		asyncio.run(self.gpt.main())
		
app = Flask(__name__)
@app.route("/", methods=['POST'])
def get_reply():
        bt = bot()
        bt.main_bot()
        #some flask method

if __name__ == "__main__":
        app.run()

I'm running edgegpt with flask,but there is an error like blow

Steps to reproduce the problem

  1. python3 main,py
  2. do something trigger get_reply() method

What should have happened?

螢幕擷取畫面 2023-07-20 104638

Version where the problem happens

EdgeGPT: 0.12.1(with 16e00af)

What Python version are you running this with?

Python: Python3.10

What is your operating system ?

Linux

Command Line Arguments

no

Console logs

[2023-07-20 10:31:53,209] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/anyio/_core/_sockets.py", line 189, in connect_tcp
    addr_obj = ip_address(remote_host)
  File "/usr/lib/python3.10/ipaddress.py", line 54, in ip_address
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
ValueError: 'www.bing.com' does not appear to be an IPv4 or IPv6 address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 257, in get_reply
    chatbot.chating(user_id, text)
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 215, in chating
    asyncio.run(self.gpt.main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 194, in main
    bot = await Chatbot.create() # Passing cookies is "optional", as explained above
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/EdgeGPT-0.12.1-py3.10.egg/EdgeGPT/EdgeGPT.py", line 42, in create
    await Conversation.create(self.proxy, cookies=cookies),
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/EdgeGPT-0.12.1-py3.10.egg/EdgeGPT/conversation.py", line 103, in create
    response = await client.get(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1757, in get
    return await self.request(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1530, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1617, in send
    response = await self._send_handling_auth(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1645, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1682, in _send_handling_redirects
    response = await self._send_single_request(request)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_client.py", line 1719, in _send_single_request
    response = await transport.handle_async_request(request)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpx/_transports/default.py", line 353, in handle_async_request
    resp = await self._pool.handle_async_request(req)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 262, in handle_async_request
    raise exc
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 245, in handle_async_request
    response = await connection.handle_async_request(request)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_async/connection.py", line 92, in handle_async_request
    raise exc
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_async/connection.py", line 69, in handle_async_request
    stream = await self._connect(request)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_async/connection.py", line 117, in _connect
    stream = await self._network_backend.connect_tcp(**kwargs)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_backends/auto.py", line 31, in connect_tcp
    return await self._backend.connect_tcp(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/httpcore/_backends/anyio.py", line 114, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/anyio/_core/_sockets.py", line 192, in connect_tcp
    gai_res = await getaddrinfo(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/anyio/_core/_sockets.py", line 511, in getaddrinfo
    gai_res = await get_asynclib().getaddrinfo(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 1690, in getaddrinfo
    result = await get_running_loop().getaddrinfo(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 860, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 818, in run_in_executor
    executor.submit(func, *args), loop=self)
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 169, in submit
    raise RuntimeError('cannot schedule new futures after '
RuntimeError: cannot schedule new futures after interpreter shutdown
127.0.0.1 - - [20/Jul/2023 10:31:53] "POST / HTTP/1.1" 500 -

Additional information

No response

@iervn6341-ovo iervn6341-ovo changed the title [Bug]: run edge with flask [Bug]: run EdgeGPT with flask Jul 20, 2023
@iervn6341-ovo
Copy link
Author

iervn6341-ovo commented Jul 20, 2023

already tried possiblilty solution

if code run normal on none flask py file,what if I define bot global variable before running flask app,then i thought change code below can be fine.

class Edgegpt():
	async def main(self):
		response = await bot.ask(prompt="introduce your self.", conversation_style=ConversationStyle.creative, simplify_response=True)
		response = response['text'].encode('utf-8').decode('utf-8')
		print(response)
		
class bot():
	def __init__(self, api):
		self.gpt = Edgegpt()
	
	def main_bot(self):
		asyncio.run(self.gpt.main())
		
app = Flask(__name__)
@app.route("/", methods=['POST'])
def get_reply():
        bt = bot()
        bt.main_bot()
        #some flask method

async def main():
	cookies = json.loads(open("./bing_cookies_.json", encoding="utf-8").read())  # might omit cookies option
	global bot
	bot = await Chatbot.create(cookies=cookies)

if __name__ == "__main__":
        asyncio.run(self.gpt.main())
        app.run()

but there also have errors

[2023-07-20 10:53:25,616] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 256, in get_reply
    chatbot.chating(user_id, text)
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 214, in chating
    asyncio.run(self.gpt.main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/user/Documents/python_workspace/request_practice/test_project/main.py", line 194, in main
    response = await bot.ask(prompt="introduce your self.", conversation_style=ConversationStyle.creative, simplify_response=True)
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/EdgeGPT-0.12.1-py3.10.egg/EdgeGPT/EdgeGPT.py", line 116, in ask
    async for final, response in self.chat_hub.ask_stream(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/EdgeGPT-0.12.1-py3.10.egg/EdgeGPT/chathub.py", line 112, in ask_stream
    async with connect(
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/websockets/legacy/client.py", line 637, in __aenter__
    return await self
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/websockets/legacy/client.py", line 655, in __await_impl_timeout__
    return await self.__await_impl__()
  File "/home/user/.local/share/virtualenvs/test_project-PUgsLNaH/lib/python3.10/site-packages/websockets/legacy/client.py", line 659, in __await_impl__
    _transport, _protocol = await self._create_connection()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1025, in create_connection
    infos = await self._ensure_resolved(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1404, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
  File "/usr/lib/python3.10/asyncio/base_events.py", line 860, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 818, in run_in_executor
    executor.submit(func, *args), loop=self)
  File "/usr/lib/python3.10/concurrent/futures/thread.py", line 169, in submit
    raise RuntimeError('cannot schedule new futures after '
RuntimeError: cannot schedule new futures after interpreter shutdown
127.0.0.1 - - [20/Jul/2023 10:53:25] "POST / HTTP/1.1" 500 -

@iervn6341-ovo
Copy link
Author

is there any way to run code without asyncio?

@opssemnik
Copy link

opssemnik commented Jul 27, 2023

You dont need asyncio to run chatgpt, flask[async] will suffice for your needs

async def setup():
global bingbot
bingbot = await Chatbot.create(...)

@app.route('/chat')
async def chat():
if not "bingbot" in globals():
await setup()
response = await bingbot.ask(...)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants