Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Hugging Face's logo
|
2 |
+
Hugging Face
|
3 |
+
Models
|
4 |
+
Datasets
|
5 |
+
Spaces
|
6 |
+
Posts
|
7 |
+
Docs
|
8 |
+
Enterprise
|
9 |
+
Pricing
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
Hugging Face is way more fun with friends and colleagues! 🤗 Join an organization
|
14 |
+
Spaces:
|
15 |
+
|
16 |
+
Qwen
|
17 |
+
/
|
18 |
+
Qwen2.5-Turbo-1M-Demo
|
19 |
+
|
20 |
+
|
21 |
+
like
|
22 |
+
359
|
23 |
+
App
|
24 |
+
Files
|
25 |
+
Community
|
26 |
+
6
|
27 |
+
Qwen2.5-Turbo-1M-Demo
|
28 |
+
/
|
29 |
+
app.py
|
30 |
+
|
31 |
+
feihu.hf
|
32 |
+
update file types
|
33 |
+
602373e
|
34 |
+
4 months ago
|
35 |
+
raw
|
36 |
+
|
37 |
+
Copy download link
|
38 |
+
history
|
39 |
+
blame
|
40 |
+
contribute
|
41 |
+
delete
|
42 |
+
|
43 |
+
3.4 kB
|
44 |
+
import os
|
45 |
+
os.system("pip install 'https://modelscope-studios.oss-cn-zhangjiakou.aliyuncs.com/SDK/gradio/gradio-4.44.0-py3-none-any.whl?OSSAccessKeyId=LTAI5tCGZWFdkWKivGKCtvTD&Expires=361727611665&Signature=iynlOFVFiaF3OmxatNMHUBPfb3o%3D'")
|
46 |
+
os.system("pip install starlette==0.38.6 fastapi==0.112.4")
|
47 |
+
|
48 |
+
from typing import List, Tuple, Union
|
49 |
+
from web_ui import WebUI
|
50 |
+
import math
|
51 |
+
|
52 |
+
from qwen_agent.agents import Assistant
|
53 |
+
from qwen_agent.tools.base import register_tool
|
54 |
+
from qwen_agent.tools.doc_parser import Record
|
55 |
+
from qwen_agent.tools.search_tools.base_search import RefMaterialOutput, BaseSearch
|
56 |
+
from qwen_agent.log import logger
|
57 |
+
from qwen_agent.gui.gradio import gr
|
58 |
+
|
59 |
+
POSITIVE_INFINITY = math.inf
|
60 |
+
|
61 |
+
@register_tool('no_search')
|
62 |
+
class NoSearch(BaseSearch):
|
63 |
+
def call(self, params: Union[str, dict], docs: List[Union[Record, str, List[str]]] = None, **kwargs) -> list:
|
64 |
+
"""The basic search algorithm
|
65 |
+
Args:
|
66 |
+
params: The dict parameters.
|
67 |
+
docs: The list of parsed doc, each doc has unique url.
|
68 |
+
Returns:
|
69 |
+
The list of retrieved chunks from each doc.
|
70 |
+
"""
|
71 |
+
params = self._verify_json_format_args(params)
|
72 |
+
# Compatible with the parameter passing of the qwen-agent version <= 0.0.3
|
73 |
+
max_ref_token = kwargs.get('max_ref_token', self.max_ref_token)
|
74 |
+
|
75 |
+
# The query is a string that may contain only the original question,
|
76 |
+
# or it may be a json string containing the generated keywords and the original question
|
77 |
+
if not docs:
|
78 |
+
return []
|
79 |
+
return self._get_the_front_part(docs, max_ref_token)
|
80 |
+
|
81 |
+
@staticmethod
|
82 |
+
def _get_the_front_part(docs: List[Record], max_ref_token: int) -> list:
|
83 |
+
all_tokens = 0
|
84 |
+
_ref_list = []
|
85 |
+
for doc in docs:
|
86 |
+
text = []
|
87 |
+
for page in doc.raw:
|
88 |
+
text.append(page.content)
|
89 |
+
all_tokens += page.token
|
90 |
+
now_ref_list = RefMaterialOutput(url=doc.url, text=text).to_dict()
|
91 |
+
_ref_list.append(now_ref_list)
|
92 |
+
|
93 |
+
logger.info(f'Using tokens: {all_tokens}')
|
94 |
+
if all_tokens > max_ref_token:
|
95 |
+
raise gr.Error(f"Your document files (around {all_tokens} tokens) exceed the maximum context length ({max_ref_token} tokens).")
|
96 |
+
return _ref_list
|
97 |
+
|
98 |
+
def sort_by_scores(self,
|
99 |
+
query: str,
|
100 |
+
docs: List[Record],
|
101 |
+
max_ref_token: int,
|
102 |
+
**kwargs) -> List[Tuple[str, int, float]]:
|
103 |
+
raise NotImplementedError
|
104 |
+
|
105 |
+
def app_gui():
|
106 |
+
# Define the agent
|
107 |
+
bot = Assistant(llm={
|
108 |
+
'model': 'qwen-turbo-1101',
|
109 |
+
'generate_cfg': {
|
110 |
+
'max_input_tokens': 1000000,
|
111 |
+
'max_retries': 10,
|
112 |
+
}},
|
113 |
+
name='Qwen-Turbo-1M',
|
114 |
+
description='Qwen-Turbo natively supports input length of up to 1M tokens. You can upload documents for Q&A (eg., pdf/docx/pptx/txt/html).',
|
115 |
+
rag_cfg={'max_ref_token': 1000000, 'rag_searchers': ['no_search']},
|
116 |
+
)
|
117 |
+
chatbot_config = {
|
118 |
+
'input.placeholder': "Type \"/clear\" to clear the history",
|
119 |
+
'verbose': True,
|
120 |
+
}
|
121 |
+
WebUI(bot, chatbot_config=chatbot_config).run()
|
122 |
+
|
123 |
+
if __name__ == '__main__':
|
124 |
+
import patching # patch qwen-agent to accelerate 1M processing
|
125 |
+
app_gui()
|
126 |
+
|