shukdevdatta123 commited on
Commit
cdbaf39
Β·
verified Β·
1 Parent(s): 6b1ecc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -49,7 +49,7 @@ class ChatbotManager:
49
  return f"βœ… Custom data integrated ({len(data_text)} characters)"
50
 
51
  def generate_response(self, user_input: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
52
- """Generate response using the selected LLM model"""
53
  if not self.current_api_key:
54
  return "❌ Please set your API key first!", history
55
 
@@ -60,13 +60,13 @@ class ChatbotManager:
60
  # Prepare conversation context
61
  messages = [{"role": "system", "content": self.system_prompt}]
62
 
63
- # Add conversation history
64
  for user_msg, assistant_msg in history:
65
- messages.append({"role": "user", "content": user_msg})
66
- messages.append({"role": "assistant", "content": assistant_msg})
67
 
68
- # Add current user input
69
- messages.append({"role": "user", "content": user_input})
70
 
71
  # Generate response
72
  response = openai.ChatCompletion.create(
@@ -79,43 +79,44 @@ class ChatbotManager:
79
  )
80
 
81
  assistant_response = response.choices[0].message.content.strip()
 
82
 
83
  # Update history
84
- history.append((user_input, assistant_response))
85
 
86
- return assistant_response, history
87
 
88
  except Exception as e:
89
  error_msg = f"❌ Error generating response: {str(e)}"
90
  return error_msg, history
91
-
92
- def clear_conversation(self) -> Tuple[str, List[Tuple[str, str]]]:
93
- """Clear conversation history"""
94
- self.conversation_history = []
95
- return "", []
96
-
97
- def export_conversation(self, history: List[Tuple[str, str]]) -> str:
98
- """Export conversation history to JSON format"""
99
- if not history:
100
- return "No conversation to export"
101
 
102
- export_data = {
103
- "timestamp": datetime.now().isoformat(),
104
- "model": self.current_model,
105
- "conversation": [
106
- {"user": user_msg, "assistant": assistant_msg}
107
- for user_msg, assistant_msg in history
108
- ]
109
- }
110
 
111
- filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
112
-
113
- try:
114
- with open(filename, 'w', encoding='utf-8') as f:
115
- json.dump(export_data, f, indent=2, ensure_ascii=False)
116
- return f"βœ… Conversation exported to {filename}"
117
- except Exception as e:
118
- return f"❌ Export failed: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  # Initialize chatbot manager
121
  chatbot = ChatbotManager()
 
49
  return f"βœ… Custom data integrated ({len(data_text)} characters)"
50
 
51
  def generate_response(self, user_input: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
52
+ """Generate response using the selected LLM model with user and AI icons"""
53
  if not self.current_api_key:
54
  return "❌ Please set your API key first!", history
55
 
 
60
  # Prepare conversation context
61
  messages = [{"role": "system", "content": self.system_prompt}]
62
 
63
+ # Add conversation history with icons
64
  for user_msg, assistant_msg in history:
65
+ messages.append({"role": "user", "content": f"πŸ‘€ {user_msg}"})
66
+ messages.append({"role": "assistant", "content": f"πŸ€– {assistant_msg}"})
67
 
68
+ # Add current user input with icon
69
+ messages.append({"role": "user", "content": f"πŸ‘€ {user_input}"})
70
 
71
  # Generate response
72
  response = openai.ChatCompletion.create(
 
79
  )
80
 
81
  assistant_response = response.choices[0].message.content.strip()
82
+ formatted_response = f"πŸ€– {assistant_response}" # Prepend AI icon to response
83
 
84
  # Update history
85
+ history.append((user_input, assistant_response)) # Store without icons for history
86
 
87
+ return formatted_response, history
88
 
89
  except Exception as e:
90
  error_msg = f"❌ Error generating response: {str(e)}"
91
  return error_msg, history
 
 
 
 
 
 
 
 
 
 
92
 
93
+ def clear_conversation(self) -> Tuple[str, List[Tuple[str, str]]]:
94
+ """Clear conversation history"""
95
+ self.conversation_history = []
96
+ return "", []
 
 
 
 
97
 
98
+ def export_conversation(self, history: List[Tuple[str, str]]) -> str:
99
+ """Export conversation history to JSON format"""
100
+ if not history:
101
+ return "No conversation to export"
102
+
103
+ export_data = {
104
+ "timestamp": datetime.now().isoformat(),
105
+ "model": self.current_model,
106
+ "conversation": [
107
+ {"user": user_msg, "assistant": assistant_msg}
108
+ for user_msg, assistant_msg in history
109
+ ]
110
+ }
111
+
112
+ filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
113
+
114
+ try:
115
+ with open(filename, 'w', encoding='utf-8') as f:
116
+ json.dump(export_data, f, indent=2, ensure_ascii=False)
117
+ return f"βœ… Conversation exported to {filename}"
118
+ except Exception as e:
119
+ return f"❌ Export failed: {str(e)}"
120
 
121
  # Initialize chatbot manager
122
  chatbot = ChatbotManager()