Manasa1 commited on
Commit
23c09c4
·
verified ·
1 Parent(s): 38f3a94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -28
app.py CHANGED
@@ -7,75 +7,87 @@ def load_llm():
7
  Loads the GPT-2 model and tokenizer using the Hugging Face `transformers` library.
8
  """
9
  try:
10
- print("Loading GPT-2 model and tokenizer...")
11
- model_name = 'gpt2' # Replace with your custom model name if using a fine-tuned version
12
  model = GPT2LMHeadModel.from_pretrained(model_name)
13
  tokenizer = GPT2Tokenizer.from_pretrained(model_name)
14
  print("Model and tokenizer successfully loaded!")
15
  return model, tokenizer
16
  except Exception as e:
17
- print(f"Error during model loading: {e}")
18
  return None, None
19
 
20
- def generate_response(model, tokenizer, user_input, max_length=512):
21
  """
22
- Generates a response using the GPT-2 model based on user input.
23
 
24
  Args:
25
- - model: The GPT-2 model.
26
- - tokenizer: The corresponding tokenizer.
27
- - user_input (str): The user's input message.
28
- - max_length (int): The maximum length of the generated output.
29
 
30
  Returns:
31
- - response (str): The AI-generated response.
32
  """
33
  try:
34
  inputs = tokenizer.encode(user_input, return_tensors='pt')
35
- outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
36
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
37
  return response
38
  except Exception as e:
39
- return f"Error during response generation: {e}"
40
 
41
  # Load the model and tokenizer
42
  model, tokenizer = load_llm()
43
 
44
  if model is None or tokenizer is None:
45
- print("Failed to load model and tokenizer.")
46
  else:
47
- print("Model and tokenizer are ready to use.")
48
 
49
- # Initialize the Hugging Face API client
50
  client = InferenceClient()
51
 
52
  def respond(message, history, system_message, max_tokens, temperature, top_p):
53
  """
54
- Handles the chatbot interaction, sending conversation history and system message
55
- to the Hugging Face Inference API for generating AI responses.
56
  """
57
- print("Respond function initiated")
58
- print("User message:", message)
59
- print("Chat history:", history)
60
 
61
- # Construct the conversation history with the system message
62
  messages = [{"role": "system", "content": system_message}]
63
 
64
  for user_msg, assistant_msg in history:
65
  if user_msg:
66
- print("Adding user message to history:", user_msg)
67
  messages.append({"role": "user", "content": user_msg})
68
  if assistant_msg:
69
- print("Adding assistant message to history:", assistant_msg)
70
  messages.append({"role": "assistant", "content": assistant_msg})
71
 
72
  messages.append({"role": "user", "content": message})
73
- print("Message list for model:", messages)
74
 
75
- # Use GPT-2 model for local generation
76
- conversation_history = " ".join([f"{msg['role']}: {msg['content']}" for msg in messages])
77
- response = generate_response(model, tokenizer, conversation_history, max_length=max_tokens)
78
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Set up the Gradio ChatInterface
81
  demo = gr.ChatInterface(
 
7
  Loads the GPT-2 model and tokenizer using the Hugging Face `transformers` library.
8
  """
9
  try:
10
+ print("Downloading or loading the GPT-2 model and tokenizer...")
11
+ model_name = 'gpt2' # Replace with your custom model if available
12
  model = GPT2LMHeadModel.from_pretrained(model_name)
13
  tokenizer = GPT2Tokenizer.from_pretrained(model_name)
14
  print("Model and tokenizer successfully loaded!")
15
  return model, tokenizer
16
  except Exception as e:
17
+ print(f"An error occurred while loading the model: {e}")
18
  return None, None
19
 
20
+ def generate_response(model, tokenizer, user_input):
21
  """
22
+ Generates a response using the GPT-2 model and tokenizer.
23
 
24
  Args:
25
+ - model: The loaded GPT-2 model.
26
+ - tokenizer: The tokenizer corresponding to the GPT-2 model.
27
+ - user_input (str): The input question from the user.
 
28
 
29
  Returns:
30
+ - response (str): The generated response.
31
  """
32
  try:
33
  inputs = tokenizer.encode(user_input, return_tensors='pt')
34
+ outputs = model.generate(inputs, max_length=512, num_return_sequences=1)
35
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
  return response
37
  except Exception as e:
38
+ return f"An error occurred during response generation: {e}"
39
 
40
  # Load the model and tokenizer
41
  model, tokenizer = load_llm()
42
 
43
  if model is None or tokenizer is None:
44
+ print("Model and/or tokenizer loading failed.")
45
  else:
46
+ print("Model and tokenizer are ready for use.")
47
 
48
+ # Initialize the Hugging Face API client (ensure it’s correctly set up)
49
  client = InferenceClient()
50
 
51
  def respond(message, history, system_message, max_tokens, temperature, top_p):
52
  """
53
+ Handles interaction with the chatbot by sending the conversation history
54
+ and system message to the Hugging Face Inference API.
55
  """
56
+ print("Starting respond function")
57
+ print("Received message:", message)
58
+ print("Conversation history:", history)
59
 
 
60
  messages = [{"role": "system", "content": system_message}]
61
 
62
  for user_msg, assistant_msg in history:
63
  if user_msg:
64
+ print("Adding user message to messages:", user_msg)
65
  messages.append({"role": "user", "content": user_msg})
66
  if assistant_msg:
67
+ print("Adding assistant message to messages:", assistant_msg)
68
  messages.append({"role": "assistant", "content": assistant_msg})
69
 
70
  messages.append({"role": "user", "content": message})
71
+ print("Final message list for the model:", messages)
72
 
73
+ response = ""
74
+ try:
75
+ for message in client.chat_completion(
76
+ messages,
77
+ max_tokens=max_tokens,
78
+ stream=True,
79
+ temperature=temperature,
80
+ top_p=top_p,
81
+ ):
82
+ token = message['choices'][0]['delta']['content']
83
+ response += token
84
+ print("Token received:", token)
85
+ yield response
86
+ except Exception as e:
87
+ print("An error occurred:", e)
88
+ yield f"An error occurred: {e}"
89
+
90
+ print("Response generation completed")
91
 
92
  # Set up the Gradio ChatInterface
93
  demo = gr.ChatInterface(