nixaut-codelabs commited on
Commit
205b3f4
·
verified ·
1 Parent(s): 6763afa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -39
app.py CHANGED
@@ -78,49 +78,41 @@ 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, 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
-
123
- return enhanced_prompt
124
  except Exception as e:
125
  return f"Enhancement failed: {str(e)}"
126
 
 
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
+ # Eğer full_response bir liste ise (örneğin [{'role': '...', 'content': '...'}, ...])
85
+ if isinstance(full_response, list):
86
+ # Assistant rolündeki son mesajı bul
87
+ assistant_messages = [msg["content"] for msg in full_response if msg["role"] == "assistant"]
88
+ if assistant_messages:
89
+ enhanced_prompt = assistant_messages[-1]
90
+ else:
91
+ enhanced_prompt = "No assistant response found."
92
+ else:
93
+ # Eğer string ise, assistant kısmını elle ayırmaya çalış
94
+ if '"role": "assistant"' in full_response:
95
+ parts = full_response.split('"role": "assistant"')
96
+ last_part = parts[-1]
97
+ if '"content":' in last_part:
98
+ start = last_part.find('"content":') + len('"content":')
99
+ content = last_part[start:].strip()
100
+ if content.startswith('"'):
101
+ end_quote = content[1:].find('"') + 1
102
+ enhanced_prompt = content[1:end_quote]
 
 
 
103
  else:
104
+ enhanced_prompt = content.split("}")[0].strip(' "\'')
105
  else:
106
+ enhanced_prompt = "Could not extract assistant content."
107
  else:
108
+ # Fallback: sadece assistant kısmını tahmin et
109
+ enhanced_prompt = full_response.strip()
110
+
111
+ # Özel token'ları temizle
112
+ enhanced_prompt = enhanced_prompt.replace("<end_of_turn>", "").strip()
113
+
114
+ return enhanced_prompt if enhanced_prompt else "No enhanced prompt generated."
115
+
 
 
 
 
 
116
  except Exception as e:
117
  return f"Enhancement failed: {str(e)}"
118