william4416 commited on
Commit
f6c0abe
·
verified ·
1 Parent(s): e6d1128

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import json
4
 
5
  # Load pre-trained model and tokenizer (replace with desired model if needed)
6
- model_name = "microsoft/DialoGPT-large"
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
@@ -49,10 +49,23 @@ def chat(message, history):
49
  response += "\nError processing the JSON data."
50
 
51
  # Update history with current conversation (optional)
52
- history.append([message, response])
53
 
54
  return response
55
 
56
- # Create Gradio interface for the chatbot
57
- interface = gr.Interface(chat, inputs="textbox", outputs="textbox", catch_exceptions=True)
58
- interface.launch(share=True) # Launch the Gradio app and share link
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import json
4
 
5
  # Load pre-trained model and tokenizer (replace with desired model if needed)
6
+ model_name = "microsoft/DialoGPT-large" # Replace with your preferred model
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
 
49
  response += "\nError processing the JSON data."
50
 
51
  # Update history with current conversation (optional)
52
+ # history.append([message, response]) # Uncomment if you want conversation history
53
 
54
  return response
55
 
56
+ # Check Gradio version and handle catch_exceptions accordingly
57
+ try:
58
+ # Option 1: Use catch_exceptions if Gradio supports it
59
+ interface = gr.Interface(chat, inputs="textbox", outputs="textbox", catch_exceptions=True)
60
+ except TypeError: # If catch_exceptions is not supported
61
+ # Option 2: Manual error handling within chat function
62
+ def chat_with_error_handling(message, history):
63
+ try:
64
+ return chat(message, history)
65
+ except Exception as e:
66
+ return f"An error occurred: {str(e)}"
67
+
68
+ interface = gr.Interface(chat_with_error_handling, inputs="textbox", outputs="textbox")
69
+
70
+ # Launch the Gradio app and share link
71
+ interface.launch(share=True)