luminoussg commited on
Commit
edb32fe
·
verified ·
1 Parent(s): 77ac272

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -26,20 +26,22 @@ def query_model(prompt, model_endpoint, system_prompt):
26
  "Content-Type": "application/json",
27
  "Accept": "application/json"
28
  }
29
- # Combine the system prompt with the user prompt
30
  formatted_prompt = f"{system_prompt}\nUser: {prompt}\nAssistant:"
31
 
 
32
  data = {
33
  "inputs": formatted_prompt,
34
  "parameters": {
35
  "max_new_tokens": 512,
36
- "temperature": 0.6, # All models use a temperature of 0.6
 
37
  }
38
  }
39
 
40
  response = requests.post(model_endpoint, headers=headers, json=data)
41
 
42
- # Uncomment the following line to print the raw API response for debugging:
43
  # print("Raw response:", response.text)
44
 
45
  try:
@@ -51,7 +53,10 @@ def query_model(prompt, model_endpoint, system_prompt):
51
  return f"Error: {result['error']}"
52
 
53
  try:
54
- return result[0].get("generated_text", "No generated_text found in response")
 
 
 
55
  except Exception:
56
  return f"Error: Unexpected response format: {json.dumps(result)}"
57
 
@@ -66,7 +71,7 @@ def chat_with_models(user_input, history):
66
  return history, history
67
 
68
  with gr.Blocks() as demo:
69
- gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API")
70
  chatbot = gr.Chatbot()
71
  msg = gr.Textbox(label="Your Message")
72
  clear = gr.Button("Clear")
 
26
  "Content-Type": "application/json",
27
  "Accept": "application/json"
28
  }
29
+ # Format the prompt to include the system instruction and structure the conversation.
30
  formatted_prompt = f"{system_prompt}\nUser: {prompt}\nAssistant:"
31
 
32
+ # Include the stop sequence so generation halts when the next user turn starts.
33
  data = {
34
  "inputs": formatted_prompt,
35
  "parameters": {
36
  "max_new_tokens": 512,
37
+ "temperature": 0.6,
38
+ "stop_sequences": ["\nUser:"]
39
  }
40
  }
41
 
42
  response = requests.post(model_endpoint, headers=headers, json=data)
43
 
44
+ # Uncomment the next line to print raw API responses for debugging.
45
  # print("Raw response:", response.text)
46
 
47
  try:
 
53
  return f"Error: {result['error']}"
54
 
55
  try:
56
+ generated_text = result[0].get("generated_text", "No generated_text found in response")
57
+ # Optionally, strip off the prompt if needed:
58
+ # generated_text = generated_text[len(formatted_prompt):].strip()
59
+ return generated_text
60
  except Exception:
61
  return f"Error: Unexpected response format: {json.dumps(result)}"
62
 
 
71
  return history, history
72
 
73
  with gr.Blocks() as demo:
74
+ gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API with Stop Sequences")
75
  chatbot = gr.Chatbot()
76
  msg = gr.Textbox(label="Your Message")
77
  clear = gr.Button("Clear")