Spaces:
Sleeping
Sleeping
william4416
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,67 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import json
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
input_ids = tokenizer(message, return_tensors="pt")["input_ids"]
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
input_ids,
|
18 |
-
max_length=512, # Adjust max_length as needed for response length
|
19 |
-
num_beams=5, # Experiment with num_beams for better phrasing
|
20 |
-
no_repeat_ngram_size=2, # Prevent repetition in responses
|
21 |
-
early_stopping=True, # Stop generation if response seems complete
|
22 |
-
)
|
23 |
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"filefourth.json": "your_key_in_filefourth",
|
33 |
-
"filefifth.json": "your_key_in_filefifth",
|
34 |
-
}
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
response += f"\nHere's some information I found in {filename}: {relevant_info}"
|
43 |
-
except FileNotFoundError:
|
44 |
-
response += f"\nCouldn't find the file: {filename}"
|
45 |
-
except json.JSONDecodeError:
|
46 |
-
response += f"\nError processing the JSON data in file: {filename}"
|
47 |
-
|
48 |
-
# Update history with current conversation (optional)
|
49 |
-
# history.append([message, response]) # Uncomment if you want conversation history
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return response
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
except TypeError: # If catch_exceptions is not supported
|
58 |
-
# Option 2: Manual error handling within chat function
|
59 |
-
def chat_with_error_handling(message, history):
|
60 |
-
try:
|
61 |
-
return chat(message, history)
|
62 |
-
except Exception as e:
|
63 |
-
return f"An error occurred: {str(e)}"
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
1 |
import json
|
2 |
+
from transformers import pipeline
|
3 |
+
import nltk
|
4 |
+
from nltk.corpus import stopwords
|
5 |
+
from nltk.tokenize import word_tokenize
|
6 |
+
from nltk.stem import WordNetLemmatizer
|
7 |
|
8 |
+
# Download NLTK resources
|
9 |
+
nltk.download('punkt')
|
10 |
+
nltk.download('wordnet')
|
11 |
+
nltk.download('stopwords')
|
12 |
|
13 |
+
# Load the JSON data from the file
|
14 |
+
with open('uts_courses.json') as f:
|
15 |
+
data = json.load(f)
|
|
|
16 |
|
17 |
+
# Load the question-answering pipeline
|
18 |
+
qa_pipeline = pipeline("question-answering")
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Define stop words and lemmatizer
|
21 |
+
stop_words = set(stopwords.words('english'))
|
22 |
+
lemmatizer = WordNetLemmatizer()
|
23 |
|
24 |
+
# Function to preprocess user input
|
25 |
+
def preprocess_input(user_input):
|
26 |
+
tokens = word_tokenize(user_input.lower())
|
27 |
+
filtered_tokens = [lemmatizer.lemmatize(word) for word in tokens if word.isalnum() and word not in stop_words]
|
28 |
+
return " ".join(filtered_tokens)
|
|
|
|
|
|
|
29 |
|
30 |
+
# Function to find courses by field of study
|
31 |
+
def find_courses_by_field(field):
|
32 |
+
if field in data['courses']:
|
33 |
+
return data['courses'][field]
|
34 |
+
else:
|
35 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Function to handle user input and generate responses
|
38 |
+
def generate_response(user_input):
|
39 |
+
user_input = preprocess_input(user_input)
|
40 |
+
if user_input == 'exit':
|
41 |
+
return "Exiting the program."
|
42 |
+
elif "courses" in user_input and "available" in user_input:
|
43 |
+
field = user_input.split("in ")[1]
|
44 |
+
courses = find_courses_by_field(field)
|
45 |
+
if courses:
|
46 |
+
response = f"Courses in {field}: {', '.join(courses)}"
|
47 |
+
else:
|
48 |
+
response = f"No courses found in {field}."
|
49 |
+
else:
|
50 |
+
answer = qa_pipeline(question=user_input, context=data)
|
51 |
+
response = answer['answer']
|
52 |
return response
|
53 |
|
54 |
+
# Main function to interact with the user
|
55 |
+
def main():
|
56 |
+
print("Welcome! I'm the UTS Course Chatbot. How can I assist you today?")
|
57 |
+
print("You can ask questions about UTS courses or type 'exit' to end the conversation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
while True:
|
60 |
+
user_input = input("You: ")
|
61 |
+
response = generate_response(user_input)
|
62 |
+
print("Bot:", response)
|
63 |
+
if response == "Exiting the program.":
|
64 |
+
break
|
65 |
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|