hsuwill000 commited on
Commit
3c3f47e
·
verified ·
1 Parent(s): 3933f4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -18
app.py CHANGED
@@ -10,13 +10,11 @@ tokenizer = AutoTokenizer.from_pretrained(model_id)
10
  # Create generation pipeline
11
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
12
 
13
- def respond(message, history):
14
  try:
15
- # Combine the entire conversation history
16
- input_text = message
17
- if history:
18
- input_text = "\n".join([f"User: {h[0]}\nBot: {h[1]}" for h in history]) + f"\nUser: {message}"
19
-
20
  # Generate response
21
  response = pipe(
22
  input_text,
@@ -28,17 +26,11 @@ def respond(message, history):
28
  )
29
  reply = response[0]['generated_text'].strip()
30
 
31
- # Update history
32
- history.append((message, reply))
33
- return history
34
 
35
  except Exception as e:
36
  print(f"Error: {e}")
37
- return history + [(message, "Sorry, something went wrong. Please try again.")]
38
-
39
- # Custom clear function
40
- def clear_history():
41
- return []
42
 
43
  # Set up Gradio chat interface
44
  with gr.Blocks() as demo:
@@ -47,10 +39,8 @@ with gr.Blocks() as demo:
47
 
48
  chatbot = gr.Chatbot()
49
  msg = gr.Textbox(label="Your Message")
50
- clear_btn = gr.Button("Clear History")
51
 
52
- msg.submit(respond, [msg, chatbot], chatbot)
53
- clear_btn.click(clear_history, None, chatbot, queue=False)
54
 
55
  if __name__ == "__main__":
56
- demo.launch()
 
10
  # Create generation pipeline
11
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
12
 
13
+ def respond(message):
14
  try:
15
+ # Only use the current message as input (no history)
16
+ input_text = f"User: {message}"
17
+
 
 
18
  # Generate response
19
  response = pipe(
20
  input_text,
 
26
  )
27
  reply = response[0]['generated_text'].strip()
28
 
29
+ return reply
 
 
30
 
31
  except Exception as e:
32
  print(f"Error: {e}")
33
+ return "Sorry, something went wrong. Please try again."
 
 
 
 
34
 
35
  # Set up Gradio chat interface
36
  with gr.Blocks() as demo:
 
39
 
40
  chatbot = gr.Chatbot()
41
  msg = gr.Textbox(label="Your Message")
 
42
 
43
+ msg.submit(respond, msg, chatbot)
 
44
 
45
  if __name__ == "__main__":
46
+ demo.launch()