Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
client = InferenceClient("Qwen/Qwen2.5-
|
|
|
5 |
|
6 |
# Global variables
|
7 |
uploaded_file_content = ""
|
8 |
-
MAX_CHARS = 50000 # Maximum characters to send to API
|
9 |
|
10 |
-
def
|
11 |
-
"""Truncate text to
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
return text
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
return text[:last_period + 1]
|
19 |
|
20 |
-
|
21 |
-
return text[:max_length] + "..."
|
22 |
|
23 |
def handle_file_upload(file_obj):
|
24 |
global uploaded_file_content
|
@@ -34,15 +37,14 @@ def handle_file_upload(file_obj):
|
|
34 |
|
35 |
# Store full content but truncate for preview
|
36 |
uploaded_file_content = file_content
|
37 |
-
|
38 |
-
preview = truncated[:200] + "..."
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
return f"""File uploaded successfully!
|
44 |
-
Total length: {
|
45 |
-
Usable length for AI: {
|
46 |
Preview of beginning:
|
47 |
{preview}"""
|
48 |
|
@@ -59,8 +61,8 @@ def respond(
|
|
59 |
):
|
60 |
global uploaded_file_content
|
61 |
|
62 |
-
# Truncate file content if needed
|
63 |
-
truncated_content =
|
64 |
|
65 |
# Format the current message to include truncated file content
|
66 |
current_message = message
|
@@ -169,4 +171,4 @@ with demo:
|
|
169 |
)
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct")
|
7 |
|
8 |
# Global variables
|
9 |
uploaded_file_content = ""
|
|
|
10 |
|
11 |
+
def truncate_text_by_tokens(text, max_tokens=2048):
|
12 |
+
"""Truncate text to max_tokens while trying to preserve complete sentences."""
|
13 |
+
# Tokenize the input text
|
14 |
+
tokens = tokenizer.encode(text)
|
15 |
+
|
16 |
+
# If the text is already within the token limit, return as is
|
17 |
+
if len(tokens) <= max_tokens:
|
18 |
return text
|
19 |
|
20 |
+
# Otherwise, truncate the token list and decode it back to text
|
21 |
+
truncated_tokens = tokens[:max_tokens]
|
22 |
+
truncated_text = tokenizer.decode(truncated_tokens, skip_special_tokens=True)
|
|
|
23 |
|
24 |
+
return truncated_text
|
|
|
25 |
|
26 |
def handle_file_upload(file_obj):
|
27 |
global uploaded_file_content
|
|
|
37 |
|
38 |
# Store full content but truncate for preview
|
39 |
uploaded_file_content = file_content
|
40 |
+
preview = truncate_text_by_tokens(file_content, max_tokens=100)[:200] + "..." # Preview truncated content
|
|
|
41 |
|
42 |
+
total_tokens = len(tokenizer.encode(file_content))
|
43 |
+
usable_tokens = len(tokenizer.encode(preview))
|
44 |
|
45 |
return f"""File uploaded successfully!
|
46 |
+
Total length: {total_tokens} tokens
|
47 |
+
Usable length for AI: {usable_tokens} tokens (due to API limits)
|
48 |
Preview of beginning:
|
49 |
{preview}"""
|
50 |
|
|
|
61 |
):
|
62 |
global uploaded_file_content
|
63 |
|
64 |
+
# Truncate file content if needed based on token limit
|
65 |
+
truncated_content = truncate_text_by_tokens(uploaded_file_content, max_tokens) if uploaded_file_content else ""
|
66 |
|
67 |
# Format the current message to include truncated file content
|
68 |
current_message = message
|
|
|
171 |
)
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
+
demo.launch()
|