Spaces:
Sleeping
Sleeping
william4416
commited on
Update app.py
Browse files
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 |
-
#
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|