Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,6 @@ def check_paywall(user_id: str):
|
|
| 21 |
if data.get("connected"):
|
| 22 |
return True, ""
|
| 23 |
else:
|
| 24 |
-
# Provide user the URL to connect their account or top up
|
| 25 |
message = f"Please connect or top up to access. Visit: {data.get('url', 'No URL provided')}"
|
| 26 |
return False, message
|
| 27 |
except Exception as e:
|
|
@@ -60,22 +59,27 @@ def chat_completion(user_id, system_message, history, user_message, max_tokens,
|
|
| 60 |
print(f"[Paywall] Chat completion error: {e}")
|
| 61 |
return f"Error retrieving completion: {e}"
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
def respond(
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
connected, paywall_msg = check_paywall(user_id)
|
| 67 |
|
| 68 |
if not connected:
|
| 69 |
-
#
|
| 70 |
-
return
|
| 71 |
|
| 72 |
-
#
|
| 73 |
reply_text = chat_completion(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
# Gradio
|
| 79 |
chatbot = gr.ChatInterface(
|
| 80 |
respond,
|
| 81 |
type="messages",
|
|
|
|
| 21 |
if data.get("connected"):
|
| 22 |
return True, ""
|
| 23 |
else:
|
|
|
|
| 24 |
message = f"Please connect or top up to access. Visit: {data.get('url', 'No URL provided')}"
|
| 25 |
return False, message
|
| 26 |
except Exception as e:
|
|
|
|
| 59 |
print(f"[Paywall] Chat completion error: {e}")
|
| 60 |
return f"Error retrieving completion: {e}"
|
| 61 |
|
| 62 |
+
# Updated respond function fit for Gradio ChatInterface
|
| 63 |
+
def respond(
|
| 64 |
+
message, history, system_message, max_tokens, temperature, top_p, hf_token
|
| 65 |
+
):
|
| 66 |
+
user_id = "demo_user" # Replace with actual user session ID
|
| 67 |
+
|
| 68 |
+
# Check paywall connection
|
| 69 |
connected, paywall_msg = check_paywall(user_id)
|
| 70 |
|
| 71 |
if not connected:
|
| 72 |
+
# When user is not connected, return empty history and paywall message string
|
| 73 |
+
return [], paywall_msg
|
| 74 |
|
| 75 |
+
# If connected, get chatbot reply
|
| 76 |
reply_text = chat_completion(user_id, system_message, history, message, max_tokens, temperature, top_p)
|
| 77 |
|
| 78 |
+
# Append new exchange to history
|
| 79 |
+
new_history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": reply_text}]
|
| 80 |
+
return new_history, "" # second return clears the input box
|
| 81 |
|
| 82 |
+
# Gradio UI setup with additional inputs
|
| 83 |
chatbot = gr.ChatInterface(
|
| 84 |
respond,
|
| 85 |
type="messages",
|