Qwen_Qwen_2_5M_Demo.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from __future__ import annotations
  2. import aiohttp
  3. import json
  4. import uuid
  5. from ...typing import AsyncResult, Messages
  6. from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
  7. from ..helper import format_prompt
  8. from ...providers.response import JsonConversation, Reasoning
  9. from ..helper import get_last_user_message
  10. from ... import debug
  11. class Qwen_Qwen_2_5M_Demo(AsyncGeneratorProvider, ProviderModelMixin):
  12. url = "https://qwen-qwen2-5-1m-demo.hf.space"
  13. api_endpoint = f"{url}/run/predict?__theme=light"
  14. working = True
  15. supports_stream = True
  16. supports_system_message = True
  17. supports_message_history = False
  18. default_model = "qwen-2.5-1m-demo"
  19. models = [default_model]
  20. @classmethod
  21. async def create_async_generator(
  22. cls,
  23. model: str,
  24. messages: Messages,
  25. proxy: str = None,
  26. return_conversation: bool = False,
  27. conversation: JsonConversation = None,
  28. **kwargs
  29. ) -> AsyncResult:
  30. def generate_session_hash():
  31. """Generate a unique session hash."""
  32. return str(uuid.uuid4()).replace('-', '')[:12]
  33. # Generate a unique session hash
  34. session_hash = generate_session_hash() if conversation is None else getattr(conversation, "session_hash")
  35. if return_conversation:
  36. yield JsonConversation(session_hash=session_hash)
  37. prompt = format_prompt(messages) if conversation is None else get_last_user_message(messages)
  38. headers = {
  39. 'accept': '*/*',
  40. 'accept-language': 'en-US',
  41. 'content-type': 'application/json',
  42. 'origin': cls.url,
  43. 'referer': f'{cls.url}/?__theme=light',
  44. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
  45. }
  46. payload_predict = {
  47. "data":[{"files":[],"text":prompt},[],[]],
  48. "event_data": None,
  49. "fn_index": 1,
  50. "trigger_id": 5,
  51. "session_hash": session_hash
  52. }
  53. async with aiohttp.ClientSession() as session:
  54. # Send join request
  55. async with session.post(cls.api_endpoint, headers=headers, json=payload_predict) as response:
  56. data = (await response.json())['data']
  57. join_url = f"{cls.url}/queue/join?__theme=light"
  58. join_data = {"data":[[[{"id":None,"elem_id":None,"elem_classes":None,"name":None,"text":prompt,"flushing":None,"avatar":"","files":[]},None]],None,0],"event_data":None,"fn_index":2,"trigger_id":5,"session_hash":session_hash}
  59. async with session.post(join_url, headers=headers, json=join_data) as response:
  60. event_id = (await response.json())['event_id']
  61. # Prepare data stream request
  62. url_data = f'{cls.url}/queue/data?session_hash={session_hash}'
  63. headers_data = {
  64. 'accept': 'text/event-stream',
  65. 'referer': f'{cls.url}/?__theme=light',
  66. 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36'
  67. }
  68. # Send data stream request
  69. async with session.get(url_data, headers=headers_data) as response:
  70. yield_response = ""
  71. yield_response_len = 0
  72. async for line in response.content:
  73. decoded_line = line.decode('utf-8')
  74. if decoded_line.startswith('data: '):
  75. try:
  76. json_data = json.loads(decoded_line[6:])
  77. # Look for generation stages
  78. if json_data.get('msg') == 'process_generating':
  79. if 'output' in json_data and 'data' in json_data['output'] and json_data['output']['data'][0]:
  80. output_data = json_data['output']['data'][0][0]
  81. if len(output_data) > 2:
  82. text = output_data[2].split("\n<summary>")[0]
  83. if text == "Qwen is thinking...":
  84. yield Reasoning(None, text)
  85. elif text.startswith(yield_response):
  86. yield text[yield_response_len:]
  87. else:
  88. yield text
  89. yield_response_len = len(text)
  90. yield_response = text
  91. # Check for completion
  92. if json_data.get('msg') == 'process_completed':
  93. # Final check to ensure we get the complete response
  94. if 'output' in json_data and 'data' in json_data['output']:
  95. output_data = json_data['output']['data'][0][0][1][0]["text"].split("\n<summary>")[0]
  96. yield output_data[yield_response_len:]
  97. yield_response_len = len(text)
  98. break
  99. except json.JSONDecodeError:
  100. debug.log("Could not parse JSON:", decoded_line)