Sg-at-srijan-us-kg commited on
Commit
ee39377
·
verified ·
1 Parent(s): b8c7368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
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-Coder-32B-Instruct")
 
5
 
6
  # Global variables
7
  uploaded_file_content = ""
8
- MAX_CHARS = 50000 # Maximum characters to send to API
9
 
10
- def truncate_text(text, max_length=MAX_CHARS):
11
- """Truncate text to max_length while trying to preserve complete sentences."""
12
- if len(text) <= max_length:
 
 
 
 
13
  return text
14
 
15
- # Try to find the last period before max_length
16
- last_period = text[:max_length].rfind('.')
17
- if last_period != -1:
18
- return text[:last_period + 1]
19
 
20
- # If no period found, just truncate at max_length
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
- truncated = truncate_text(file_content, MAX_CHARS) # Store full but preview truncated
38
- preview = truncated[:200] + "..."
39
 
40
- total_chars = len(file_content)
41
- usable_chars = len(truncated)
42
 
43
  return f"""File uploaded successfully!
44
- Total length: {total_chars} characters
45
- Usable length for AI: {usable_chars} characters (due to API limits)
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 = truncate_text(uploaded_file_content) if uploaded_file_content else ""
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()