HydroFlyer53 commited on
Commit
8397087
·
verified ·
1 Parent(s): 1633001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -9,12 +9,18 @@ client = Client("HydroFlyer53/ThePickle", hf_token=os.environ["Key"])
9
  def chat_with_ai(message, history):
10
  """Function to get AI response from Hugging Face model with limited history tracking."""
11
 
12
- # Retain only the last 3 exchanges in history
13
  trimmed_history = history[-3:] if history else []
14
- formatted_history = "\n".join([f"User: {h[0]}\nAI: {h[1]}" for h in trimmed_history])
 
 
 
 
15
 
16
  # Combine history with new message
17
  prompt = f"{formatted_history}\nUser: {message}\nAI:"
 
 
18
 
19
  result = client.predict(
20
  message=prompt,
@@ -30,7 +36,11 @@ def chat_with_ai(message, history):
30
  top_p=0.60,
31
  api_name="/chat"
32
  )
33
-
 
 
 
 
34
  return result
35
 
36
  # Gradio Chat Interface with history
 
9
  def chat_with_ai(message, history):
10
  """Function to get AI response from Hugging Face model with limited history tracking."""
11
 
12
+ # Retain only the last 3 exchanges in history and ensure proper formatting
13
  trimmed_history = history[-3:] if history else []
14
+ formatted_history = "\n".join([f"User: {h[0]}\nAI: {h[1]}" for h in trimmed_history if h])
15
+
16
+ # Ensure the history is not empty or corrupted
17
+ if not formatted_history.strip():
18
+ formatted_history = "User: Hi\nAI: What's up?"
19
 
20
  # Combine history with new message
21
  prompt = f"{formatted_history}\nUser: {message}\nAI:"
22
+
23
+ print("DEBUG: Prompt sent to AI:", prompt) # Debugging line to check input
24
 
25
  result = client.predict(
26
  message=prompt,
 
36
  top_p=0.60,
37
  api_name="/chat"
38
  )
39
+
40
+ # Prevent endless repetition
41
+ if result.strip().count("Sus AI:") > 3:
42
+ return "Yo, I think I glitched out—let’s try that again!"
43
+
44
  return result
45
 
46
  # Gradio Chat Interface with history