Spaces:
Sleeping
Sleeping
File size: 2,404 Bytes
37a9bbd 5633877 5ce1dbc af8384f 743067f e6d1128 37a9bbd 5ce1dbc 23a6dc1 6e93ad1 5633877 37a9bbd e6d1128 37a9bbd 5633877 37a9bbd af8384f 37a9bbd 5ce1dbc 37a9bbd 5ce1dbc 37a9bbd 2e7fa77 37a9bbd 2e7fa77 37a9bbd 2e7fa77 37a9bbd 6e93ad1 37a9bbd 2e7fa77 23c5a04 37a9bbd 5ce1dbc 37a9bbd 5ce1dbc 5633877 37a9bbd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# Import required libraries
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import json
# Create FastAPI app instance
app = FastAPI()
# Load DialoGPT model and tokenizer
try:
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Model loading failed: {e}")
# Load courses data
try:
with open("uts_courses.json", "r") as file:
courses_data = json.load(file)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Courses data loading failed: {e}")
# Define user input model
class UserInput(BaseModel):
user_input: str
# Generate response function
def generate_response(user_input: str):
"""
Generate response based on user input
Args:
user_input: User input text
Returns:
Generated response text
"""
if user_input.lower() == "help":
return "I can help you with UTS courses information, feel free to ask!"
elif user_input.lower() == "exit":
return "Goodbye!"
elif user_input.lower() == "list courses":
# Generate course list
course_list = "\n".join([f"{category}: {', '.join(courses)}" for category, courses in courses_data["courses"].items()])
return f"Here are the available courses:\n{course_list}"
elif user_input.lower() in courses_data["courses"]:
# List courses under the specified course category
return f"The courses in {user_input} category are: {', '.join(courses_data['courses'][user_input])}"
else:
# Use DialoGPT model to generate response
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
response_ids = model.generate(input_ids, max_length=100, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
return response
# Define API route
@app.post("/")
async def chat(user_input: UserInput):
"""
Process user input and return response
Args:
user_input: User input JSON data
Returns:
JSON data containing the response text
"""
response = generate_response(user_input.user_input)
return {"response": response}
|