Update app.py
Browse files
app.py
CHANGED
|
@@ -2903,9 +2903,8 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 2903 |
save_chat_history(email, message, "")
|
| 2904 |
|
| 2905 |
try:
|
| 2906 |
-
# Save to BATTERY_SHEET_ID
|
| 2907 |
sheet = get_or_create_sheet(BATTERY_SHEET_ID, "ChatHistory")
|
| 2908 |
-
|
| 2909 |
now_str = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
|
| 2910 |
sheet.append_row([now_str, email, message, ""])
|
| 2911 |
|
|
@@ -2915,7 +2914,7 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 2915 |
temp_response = f"""<a href="https://flutterwave.com/pay/gossapp" target="_blank">
|
| 2916 |
<img src='https://i.imgur.com/PznT4qo.png' width='20'>
|
| 2917 |
</a>"""
|
| 2918 |
-
yield [(message, temp_response)], None
|
| 2919 |
|
| 2920 |
try:
|
| 2921 |
# Build prompt
|
|
@@ -2923,7 +2922,7 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 2923 |
for user_msg, bot_msg in history:
|
| 2924 |
if user_msg:
|
| 2925 |
messages.append({"role": "user", "content": user_msg})
|
| 2926 |
-
if bot_msg and not str(bot_msg).startswith("<img"):
|
| 2927 |
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 2928 |
messages.append({"role": "user", "content": message})
|
| 2929 |
|
|
@@ -2931,18 +2930,27 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 2931 |
inference_client = get_inference_client()
|
| 2932 |
|
| 2933 |
response = ""
|
| 2934 |
-
|
| 2935 |
messages,
|
| 2936 |
max_tokens=max_tokens,
|
| 2937 |
temperature=temperature,
|
| 2938 |
top_p=top_p,
|
| 2939 |
stream=True
|
| 2940 |
-
)
|
| 2941 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2942 |
if token:
|
| 2943 |
response += token
|
| 2944 |
clean_response = response.split("(Note:")[0].strip()
|
| 2945 |
-
yield [(message, clean_response)], None
|
| 2946 |
|
| 2947 |
clean_response = response.split("(Note:")[0].strip()
|
| 2948 |
|
|
@@ -2957,21 +2965,19 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 2957 |
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
| 2958 |
'https://www.googleapis.com/auth/drive']
|
| 2959 |
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
|
| 2960 |
-
gspread_client = gspread.authorize(creds)
|
| 2961 |
|
| 2962 |
spreadsheet = gspread_client.open_by_key(BATTERY_SHEET_ID)
|
| 2963 |
|
| 2964 |
-
# Search all sheets for our record to update
|
| 2965 |
for worksheet in spreadsheet.worksheets():
|
| 2966 |
try:
|
| 2967 |
records = worksheet.get_all_records()
|
| 2968 |
-
for i, record in enumerate(records, start=2):
|
| 2969 |
if (str(record.get('Email', '')).lower() == email.lower() and
|
| 2970 |
str(record.get('User Message', '')).strip() == message.strip() and
|
| 2971 |
(not record.get('Bot Message') or str(record.get('Bot Message', '')).strip() == "")):
|
| 2972 |
|
| 2973 |
-
|
| 2974 |
-
worksheet.update_cell(i, 4, clean_response) # Column D is index 4
|
| 2975 |
break
|
| 2976 |
except Exception as e:
|
| 2977 |
print(f"Error searching sheet {worksheet.title}: {str(e)}")
|
|
@@ -3003,7 +3009,6 @@ def respond(message, history, max_tokens, temperature, top_p):
|
|
| 3003 |
save_chat_history(email, message, f"Error occurred: {str(e)}")
|
| 3004 |
yield [(message, custom_error)], None
|
| 3005 |
|
| 3006 |
-
|
| 3007 |
|
| 3008 |
def clear_input():
|
| 3009 |
return ""
|
|
|
|
| 2903 |
save_chat_history(email, message, "")
|
| 2904 |
|
| 2905 |
try:
|
| 2906 |
+
# Save to BATTERY_SHEET_ID
|
| 2907 |
sheet = get_or_create_sheet(BATTERY_SHEET_ID, "ChatHistory")
|
|
|
|
| 2908 |
now_str = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
|
| 2909 |
sheet.append_row([now_str, email, message, ""])
|
| 2910 |
|
|
|
|
| 2914 |
temp_response = f"""<a href="https://flutterwave.com/pay/gossapp" target="_blank">
|
| 2915 |
<img src='https://i.imgur.com/PznT4qo.png' width='20'>
|
| 2916 |
</a>"""
|
| 2917 |
+
yield [(message, temp_response)], None
|
| 2918 |
|
| 2919 |
try:
|
| 2920 |
# Build prompt
|
|
|
|
| 2922 |
for user_msg, bot_msg in history:
|
| 2923 |
if user_msg:
|
| 2924 |
messages.append({"role": "user", "content": user_msg})
|
| 2925 |
+
if bot_msg and not str(bot_msg).startswith("<img"):
|
| 2926 |
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 2927 |
messages.append({"role": "user", "content": message})
|
| 2928 |
|
|
|
|
| 2930 |
inference_client = get_inference_client()
|
| 2931 |
|
| 2932 |
response = ""
|
| 2933 |
+
stream = inference_client.chat_completion(
|
| 2934 |
messages,
|
| 2935 |
max_tokens=max_tokens,
|
| 2936 |
temperature=temperature,
|
| 2937 |
top_p=top_p,
|
| 2938 |
stream=True
|
| 2939 |
+
)
|
| 2940 |
+
|
| 2941 |
+
for chunk in stream:
|
| 2942 |
+
# Handle both old and new API response formats
|
| 2943 |
+
if hasattr(chunk, 'token') and chunk.token.text: # Old format
|
| 2944 |
+
token = chunk.token.text
|
| 2945 |
+
elif hasattr(chunk, 'choices') and chunk.choices: # New format
|
| 2946 |
+
token = chunk.choices[0].delta.content or ""
|
| 2947 |
+
else:
|
| 2948 |
+
continue
|
| 2949 |
+
|
| 2950 |
if token:
|
| 2951 |
response += token
|
| 2952 |
clean_response = response.split("(Note:")[0].strip()
|
| 2953 |
+
yield [(message, clean_response)], None
|
| 2954 |
|
| 2955 |
clean_response = response.split("(Note:")[0].strip()
|
| 2956 |
|
|
|
|
| 2965 |
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
| 2966 |
'https://www.googleapis.com/auth/drive']
|
| 2967 |
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
|
| 2968 |
+
gspread_client = gspread.authorize(creds)
|
| 2969 |
|
| 2970 |
spreadsheet = gspread_client.open_by_key(BATTERY_SHEET_ID)
|
| 2971 |
|
|
|
|
| 2972 |
for worksheet in spreadsheet.worksheets():
|
| 2973 |
try:
|
| 2974 |
records = worksheet.get_all_records()
|
| 2975 |
+
for i, record in enumerate(records, start=2):
|
| 2976 |
if (str(record.get('Email', '')).lower() == email.lower() and
|
| 2977 |
str(record.get('User Message', '')).strip() == message.strip() and
|
| 2978 |
(not record.get('Bot Message') or str(record.get('Bot Message', '')).strip() == "")):
|
| 2979 |
|
| 2980 |
+
worksheet.update_cell(i, 4, clean_response)
|
|
|
|
| 2981 |
break
|
| 2982 |
except Exception as e:
|
| 2983 |
print(f"Error searching sheet {worksheet.title}: {str(e)}")
|
|
|
|
| 3009 |
save_chat_history(email, message, f"Error occurred: {str(e)}")
|
| 3010 |
yield [(message, custom_error)], None
|
| 3011 |
|
|
|
|
| 3012 |
|
| 3013 |
def clear_input():
|
| 3014 |
return ""
|