Reality123b commited on
Commit
ab73210
·
verified ·
1 Parent(s): e49806f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -77,8 +77,15 @@ def user_message(message, history):
77
  if message is None or message == "":
78
  return "", history
79
 
80
- # Add user message to history
81
- history = history + [(message, None)]
 
 
 
 
 
 
 
82
  return "", history
83
 
84
  def bot_response(history, system_msg, max_tokens, temperature, top_p):
@@ -86,9 +93,10 @@ def bot_response(history, system_msg, max_tokens, temperature, top_p):
86
  if not history:
87
  return history, None
88
 
89
- last_user_message = history[-1][0]
90
-
91
  try:
 
 
 
92
  # Process the message for language and script
93
  processed_message = detect_and_transliterate(last_user_message)
94
  translated_message = translate_to_english(processed_message)
@@ -97,8 +105,11 @@ def bot_response(history, system_msg, max_tokens, temperature, top_p):
97
  if check_for_image_generation(translated_message):
98
  # Generate image based on the message
99
  image = generate_image(translated_message)
100
- # Update history with a confirmation message
101
- history[-1] = (history[-1][0], "I've generated an image based on your request.")
 
 
 
102
  return history, image
103
 
104
  # Prepare chat messages for the model
@@ -107,14 +118,8 @@ def bot_response(history, system_msg, max_tokens, temperature, top_p):
107
  ]
108
 
109
  # Add conversation history
110
- for msg in history[:-1]: # Exclude the last message pair
111
- if msg[0]: # User message
112
- messages.append({"role": "user", "content": msg[0]})
113
- if msg[1]: # Assistant message
114
- messages.append({"role": "assistant", "content": msg[1]})
115
-
116
- # Add the last user message
117
- messages.append({"role": "user", "content": translated_message})
118
 
119
  # Get model response
120
  response = text_client.text_generation(
@@ -125,16 +130,22 @@ def bot_response(history, system_msg, max_tokens, temperature, top_p):
125
  repetition_penalty=1.1,
126
  )
127
 
128
- # Update history with bot response
129
- history[-1] = (history[-1][0], response)
 
 
 
130
 
131
  except Exception as e:
132
  # Handle errors gracefully
133
  error_message = f"I apologize, but I encountered an error: {str(e)}"
134
- history[-1] = (history[-1][0], error_message)
 
 
 
135
 
136
  return history, None
137
-
138
  def create_chat_interface():
139
  # Custom CSS for better styling with Inter font and animations
140
  custom_css = """
 
77
  if message is None or message == "":
78
  return "", history
79
 
80
+ # Convert history to list if it's None
81
+ history = history or []
82
+
83
+ # Add user message to history with proper format
84
+ history.append({
85
+ "role": "user",
86
+ "content": message
87
+ })
88
+
89
  return "", history
90
 
91
  def bot_response(history, system_msg, max_tokens, temperature, top_p):
 
93
  if not history:
94
  return history, None
95
 
 
 
96
  try:
97
+ # Get the last user message
98
+ last_user_message = history[-1]["content"]
99
+
100
  # Process the message for language and script
101
  processed_message = detect_and_transliterate(last_user_message)
102
  translated_message = translate_to_english(processed_message)
 
105
  if check_for_image_generation(translated_message):
106
  # Generate image based on the message
107
  image = generate_image(translated_message)
108
+ # Add bot response with proper format
109
+ history.append({
110
+ "role": "assistant",
111
+ "content": "I've generated an image based on your request."
112
+ })
113
  return history, image
114
 
115
  # Prepare chat messages for the model
 
118
  ]
119
 
120
  # Add conversation history
121
+ for msg in history:
122
+ messages.append(msg)
 
 
 
 
 
 
123
 
124
  # Get model response
125
  response = text_client.text_generation(
 
130
  repetition_penalty=1.1,
131
  )
132
 
133
+ # Add bot response with proper format
134
+ history.append({
135
+ "role": "assistant",
136
+ "content": response
137
+ })
138
 
139
  except Exception as e:
140
  # Handle errors gracefully
141
  error_message = f"I apologize, but I encountered an error: {str(e)}"
142
+ history.append({
143
+ "role": "assistant",
144
+ "content": error_message
145
+ })
146
 
147
  return history, None
148
+
149
  def create_chat_interface():
150
  # Custom CSS for better styling with Inter font and animations
151
  custom_css = """