ramzan118 commited on
Commit
f1fac87
·
verified ·
1 Parent(s): ac26782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -29
app.py CHANGED
@@ -1,44 +1,37 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Load DialoGPT model and tokenizer
6
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
7
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
8
 
9
- # Define chatbot function using OpenAI-style message format
10
- def chat_with_bot(message, history):
11
- # Convert history to a flat string
12
- conversation = ""
13
- for turn in history:
14
- if turn["role"] == "user":
15
- conversation += turn["content"] + tokenizer.eos_token
16
- elif turn["role"] == "assistant":
17
- conversation += turn["content"] + tokenizer.eos_token
18
 
19
- # Append current user message
20
- conversation += message + tokenizer.eos_token
 
21
 
22
  # Tokenize and generate
23
- input_ids = tokenizer.encode(conversation, return_tensors="pt")
24
  output_ids = model.generate(
25
  input_ids,
26
- max_length=1000,
27
- pad_token_id=tokenizer.eos_token_id,
28
  do_sample=True,
29
- top_k=50,
30
- top_p=0.95
31
  )
32
-
33
- # Decode only the new response
34
- response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
35
-
36
  return {"role": "assistant", "content": response}
37
 
38
  # Launch the chatbot
39
  gr.ChatInterface(
40
- fn=chat_with_bot,
41
- title="Muhammad’s ChatGPT",
42
- description="A simple chatbot powered by DialoGPT-small. Ask me anything!",
43
- type="messages" # Important for compatibility with message dicts
44
  ).launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
3
 
4
+ # Load GODEL model and tokenizer
5
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
7
 
8
+ # Define the chatbot function
9
+ def generate_response(message, history):
10
+ # Format dialog history
11
+ dialog = [turn["content"] for turn in history if turn["role"] == "user"]
12
+ dialog.append(message)
13
+ dialog_text = " EOS ".join(dialog)
 
 
 
14
 
15
+ # GODEL expects an instruction and context
16
+ instruction = "Instruction: given a dialog context, respond appropriately."
17
+ query = f"{instruction} [CONTEXT] {dialog_text}"
18
 
19
  # Tokenize and generate
20
+ input_ids = tokenizer(query, return_tensors="pt").input_ids
21
  output_ids = model.generate(
22
  input_ids,
23
+ max_length=128,
 
24
  do_sample=True,
25
+ top_p=0.9,
26
+ temperature=0.7
27
  )
28
+ response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
 
 
29
  return {"role": "assistant", "content": response}
30
 
31
  # Launch the chatbot
32
  gr.ChatInterface(
33
+ fn=generate_response,
34
+ title="Muhammad’s GODEL Chatbot",
35
+ description="A grounded chatbot powered by Microsoft's GODEL model.",
36
+ type="messages"
37
  ).launch()