SanaAdeel commited on
Commit
a201e77
·
verified ·
1 Parent(s): 5a9deb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
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
- # Full respond function used by Gradio chatbot interface
64
- def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token):
65
- user_id = "demo_user" # Replace or dynamically assign per user session
 
 
 
 
66
  connected, paywall_msg = check_paywall(user_id)
67
 
68
  if not connected:
69
- # User not connected. Show connection URL or message.
70
- return (history, paywall_msg)
71
 
72
- # User connected - forward chat message to paywalls chat completions endpoint
73
  reply_text = chat_completion(user_id, system_message, history, message, max_tokens, temperature, top_p)
74
 
75
- history = history + [{"role": "user", "content": message}, {"role": "assistant", "content": reply_text}]
76
- return history, ""
 
77
 
78
- # Gradio interface setup
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",