Utkarsh Verma commited on
Commit
ad15d33
Β·
1 Parent(s): 688b851

Code Change

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -29,7 +29,7 @@ def chat():
29
  "temperature": 0.5, # πŸ”₯ Controls randomness (lower = more deterministic)
30
  "top_p": 0.9, # 🎯 Focus on high-probability words
31
  "max_new_tokens": 50, # ⏳ Limits response length
32
- "stop_sequences": ["User:"] # β›” Stops response at natural points
33
  }
34
  }
35
  response = requests.post(API_URL, headers=headers, json=payload)
@@ -47,10 +47,23 @@ def chat():
47
  if "error" in data:
48
  return jsonify({"reply": f"Error: {data['error']}"})
49
 
50
- raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
51
- reply = raw_output.split("Bot:")[-1].strip()
52
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  return jsonify({"reply": reply})
55
 
56
  except Exception as e:
 
29
  "temperature": 0.5, # πŸ”₯ Controls randomness (lower = more deterministic)
30
  "top_p": 0.9, # 🎯 Focus on high-probability words
31
  "max_new_tokens": 50, # ⏳ Limits response length
32
+ "stop_sequences": ["User:", "You:", "user:"] # β›” Stops response at natural points
33
  }
34
  }
35
  response = requests.post(API_URL, headers=headers, json=payload)
 
47
  if "error" in data:
48
  return jsonify({"reply": f"Error: {data['error']}"})
49
 
50
+ # raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
51
+ # reply = raw_output.split("Bot:")[-1].strip()
52
 
53
 
54
+ # return jsonify({"reply": reply})
55
+
56
+ raw_output = data[0]['generated_text'] if isinstance(data, list) else data.get('generated_text', "No response")
57
+ # Clean up output
58
+ if "Bot:" in raw_output:
59
+ reply = raw_output.split("Bot:")[-1].strip()
60
+ else:
61
+ reply = raw_output.strip()
62
+
63
+ for trailing in ["User", "You", "User:", "You:"]:
64
+ if reply.endswith(trailing):
65
+ reply = reply.rsplit(trailing, 1)[0].strip()
66
+
67
  return jsonify({"reply": reply})
68
 
69
  except Exception as e: