nixaut-codelabs commited on
Commit
473b785
·
verified ·
1 Parent(s): bb3562d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -78,24 +78,45 @@ def enhance_for_gradio(prompt_text, api_key):
78
  ]
79
 
80
  try:
81
- result = pipe(messages, max_new_tokens=256, temperature=0.7, do_sample=True)
82
  full_response = result[0]["generated_text"]
83
 
84
- # Extract only the new generated content after the original messages
85
  if isinstance(full_response, str):
86
- # Find the assistant's response after the user message
87
- parts = full_response.split("<end_of_turn>")
88
- if len(parts) > 2:
89
- enhanced_prompt = parts[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  else:
91
- # Fallback: try to extract after last user message
92
- user_msg_end = full_response.rfind(prompt_text)
93
- if user_msg_end != -1:
94
- enhanced_prompt = full_response[user_msg_end + len(prompt_text):].strip()
95
- # Clean up any remaining tokens
96
- enhanced_prompt = enhanced_prompt.replace("<end_of_turn>", "").strip()
 
97
  else:
98
- enhanced_prompt = full_response
99
  else:
100
  enhanced_prompt = str(full_response)
101
 
 
78
  ]
79
 
80
  try:
81
+ result = pipe(messages, max_new_tokens=256, temperature=0.7, do_sample=True, pad_token_id=pipe.tokenizer.eos_token_id)
82
  full_response = result[0]["generated_text"]
83
 
84
+ # Extract only the assistant's response
85
  if isinstance(full_response, str):
86
+ # Split by the conversation structure and get the last assistant response
87
+ if "assistant" in full_response:
88
+ # Find the last occurrence of assistant role
89
+ assistant_parts = full_response.split('"role": "assistant"')
90
+ if len(assistant_parts) > 1:
91
+ last_part = assistant_parts[-1]
92
+ # Extract content between quotes
93
+ if '"content":' in last_part:
94
+ content_start = last_part.find('"content":') + len('"content":')
95
+ content_part = last_part[content_start:].strip()
96
+ # Find the actual content between quotes
97
+ if content_part.startswith('"') or content_part.startswith("'"):
98
+ quote_char = content_part[0]
99
+ end_quote = content_part.find(quote_char, 1)
100
+ if end_quote > 0:
101
+ enhanced_prompt = content_part[1:end_quote]
102
+ else:
103
+ enhanced_prompt = content_part[1:].split('}')[0].strip()
104
+ else:
105
+ enhanced_prompt = content_part.split('}')[0].strip(' "\'')
106
+ else:
107
+ enhanced_prompt = "Unable to extract enhanced prompt"
108
+ else:
109
+ enhanced_prompt = "No assistant response found"
110
  else:
111
+ # Fallback: try to get content after user input
112
+ user_content = prompt_text
113
+ if user_content in full_response:
114
+ response_start = full_response.rfind(user_content) + len(user_content)
115
+ remaining = full_response[response_start:].strip()
116
+ # Clean up response
117
+ enhanced_prompt = remaining.replace('"', '').replace("'", "").replace("}", "").strip()[:500]
118
  else:
119
+ enhanced_prompt = "Unable to parse response"
120
  else:
121
  enhanced_prompt = str(full_response)
122