Spaces:
Running
Running
milwright
commited on
Commit
·
144ad8f
1
Parent(s):
8e528af
fix duplicate export function and sync imports between app.py and utils.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ from pathlib import Path
|
|
| 18 |
# Import our shared utilities
|
| 19 |
from utils import (
|
| 20 |
get_theme, fetch_url_content, create_safe_filename,
|
| 21 |
-
|
| 22 |
ConfigurationManager, get_model_choices, AVAILABLE_THEMES,
|
| 23 |
extract_urls_from_text
|
| 24 |
)
|
|
|
|
| 18 |
# Import our shared utilities
|
| 19 |
from utils import (
|
| 20 |
get_theme, fetch_url_content, create_safe_filename,
|
| 21 |
+
export_conversation_to_text, process_file_upload,
|
| 22 |
ConfigurationManager, get_model_choices, AVAILABLE_THEMES,
|
| 23 |
extract_urls_from_text
|
| 24 |
)
|
utils.py
CHANGED
|
@@ -143,40 +143,53 @@ def create_safe_filename(base_name: str, suffix: str = ".md", prefix: str = "",
|
|
| 143 |
return f"{safe_name}{suffix}"
|
| 144 |
|
| 145 |
|
| 146 |
-
def
|
| 147 |
-
|
| 148 |
-
"""Export conversation history to
|
| 149 |
if not history:
|
| 150 |
return "No conversation to export."
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 155 |
-
"""
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
Model: {
|
|
|
|
|
|
|
|
|
|
| 162 |
"""
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Process messages
|
| 167 |
-
message_pair_count = 0
|
| 168 |
for message in history:
|
| 169 |
if isinstance(message, dict):
|
| 170 |
role = message.get('role', 'unknown')
|
| 171 |
content = message.get('content', '')
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
if role == 'user':
|
| 174 |
-
|
| 175 |
-
|
| 176 |
elif role == 'assistant':
|
| 177 |
-
|
| 178 |
|
| 179 |
-
return
|
| 180 |
|
| 181 |
|
| 182 |
def process_file_upload(file_path: str, max_chars: int = 8000) -> str:
|
|
|
|
| 143 |
return f"{safe_name}{suffix}"
|
| 144 |
|
| 145 |
|
| 146 |
+
def export_conversation_to_text(history: List[Dict[str, str]],
|
| 147 |
+
config: Optional[Dict[str, Any]] = None) -> str:
|
| 148 |
+
"""Export conversation history to text with timestamps"""
|
| 149 |
if not history:
|
| 150 |
return "No conversation to export."
|
| 151 |
|
| 152 |
+
space_name = config.get('name', 'AI Assistant') if config else 'AI Assistant'
|
| 153 |
+
model_name = config.get('model', 'Unknown') if config else 'Unknown'
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
text_content = f"""Conversation Export
|
| 156 |
+
==================
|
| 157 |
+
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 158 |
+
Space: {space_name}
|
| 159 |
+
Model: {model_name}
|
| 160 |
+
|
| 161 |
+
==================
|
| 162 |
+
|
| 163 |
"""
|
| 164 |
|
| 165 |
+
message_count = 0
|
|
|
|
|
|
|
|
|
|
| 166 |
for message in history:
|
| 167 |
if isinstance(message, dict):
|
| 168 |
role = message.get('role', 'unknown')
|
| 169 |
content = message.get('content', '')
|
| 170 |
|
| 171 |
+
# Get timestamp from message or use current time as fallback
|
| 172 |
+
timestamp_str = message.get('timestamp', '')
|
| 173 |
+
if timestamp_str:
|
| 174 |
+
try:
|
| 175 |
+
# Parse ISO format timestamp and format it nicely
|
| 176 |
+
timestamp = datetime.fromisoformat(timestamp_str)
|
| 177 |
+
formatted_timestamp = timestamp.strftime('%Y-%m-%d %H:%M:%S')
|
| 178 |
+
except:
|
| 179 |
+
formatted_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 180 |
+
else:
|
| 181 |
+
formatted_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 182 |
+
|
| 183 |
+
# Get message length
|
| 184 |
+
msg_length = message.get('length', len(content))
|
| 185 |
+
|
| 186 |
if role == 'user':
|
| 187 |
+
message_count += 1
|
| 188 |
+
text_content += f"[{formatted_timestamp}] User Message {message_count} ({msg_length} chars):\n{content}\n\n"
|
| 189 |
elif role == 'assistant':
|
| 190 |
+
text_content += f"[{formatted_timestamp}] Assistant Response {message_count} ({msg_length} chars):\n{content}\n\n------------------\n\n"
|
| 191 |
|
| 192 |
+
return text_content
|
| 193 |
|
| 194 |
|
| 195 |
def process_file_upload(file_path: str, max_chars: int = 8000) -> str:
|