Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 82 |
full_response = result[0]["generated_text"]
|
| 83 |
-
|
| 84 |
-
#
|
| 85 |
-
if isinstance(full_response,
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
enhanced_prompt = content_part[1:].split('}')[0].strip()
|
| 104 |
-
else:
|
| 105 |
-
enhanced_prompt = content_part.split('}')[0].strip(' "\'')
|
| 106 |
else:
|
| 107 |
-
enhanced_prompt = "
|
| 108 |
else:
|
| 109 |
-
enhanced_prompt = "
|
| 110 |
else:
|
| 111 |
-
# Fallback:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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 |
|