redael commited on
Commit
b12166e
·
verified ·
1 Parent(s): 52291b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -32
app.py CHANGED
@@ -1,57 +1,77 @@
1
 
 
 
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
- import gradio as gr
5
 
6
- model_name = 'redael/model_udc'
7
-
8
- # Load the tokenizer and model from Hugging Face
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForCausalLM.from_pretrained(model_name)
 
11
 
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
  model.to(device)
14
 
15
- # Define the generate_response function
16
- def generate_response(prompt, model=model, tokenizer=tokenizer, max_length=100, num_beams=5, temperature=0.5, top_p=0.9, repetition_penalty=4.0):
17
- # Add context to the prompt
18
- prompt = f"User: {prompt}\nAssistant:"
19
-
 
 
 
 
 
 
 
 
 
 
20
  inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=512).to(device)
 
 
21
  outputs = model.generate(
22
  inputs['input_ids'],
23
- max_length=max_length,
24
  num_return_sequences=1,
25
  pad_token_id=tokenizer.eos_token_id,
26
- num_beams=num_beams,
27
  temperature=temperature,
28
  top_p=top_p,
29
- repetition_penalty=repetition_penalty,
30
  early_stopping=True
31
  )
32
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
 
34
- # Clean up the response to make it more natural
35
- response = response.replace('User:', '').replace('Assistant:', '').strip()
36
- return response
37
-
38
- # Define the Gradio interface
39
- def chatbot_interface(user_input):
40
- return generate_response(user_input)
 
 
 
41
 
42
- iface = gr.Interface(
43
- fn=chatbot_interface,
44
- inputs="text",
45
- outputs="text",
46
- title="Chatbot",
47
- description="Ask anything to the chatbot.",
48
- examples=[
49
- ["What is the command to get a list of all files in /var/cache/apt/archives?"],
50
- ["How do I change to root in Ubuntu?"],
51
- ["What are the best practices for securing a web server?"]
 
 
 
 
52
  ],
53
- live=True
 
54
  )
55
 
56
- # Launch the Gradio interface
57
- iface.launch()
 
1
 
2
+ model_name = 'redael/model_udc'
3
+ import os
4
+ import gradio as gr
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
  import torch
 
7
 
8
+ # Load your model and tokenizer from Hugging Face
9
+ print("l.......")
 
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  model = AutoModelForCausalLM.from_pretrained(model_name)
12
+ print("done")
13
 
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
  model.to(device)
16
 
17
+ # Function to generate response
18
+ def generate_response(message, history, system_message, max_tokens, temperature, top_p):
19
+ # Prepare the conversation history
20
+ messages = [{"role": "system", "content": system_message}]
21
+
22
+ for user_msg, bot_msg in history:
23
+ if user_msg:
24
+ messages.append({"role": "user", "content": user_msg})
25
+ if bot_msg:
26
+ messages.append({"role": "assistant", "content": bot_msg})
27
+
28
+ messages.append({"role": "user", "content": message})
29
+
30
+ # Tokenize and prepare the input
31
+ prompt = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages])
32
  inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=512).to(device)
33
+
34
+ # Generate the response
35
  outputs = model.generate(
36
  inputs['input_ids'],
37
+ max_length=max_tokens,
38
  num_return_sequences=1,
39
  pad_token_id=tokenizer.eos_token_id,
 
40
  temperature=temperature,
41
  top_p=top_p,
 
42
  early_stopping=True
43
  )
44
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
 
46
+ # Clean up the response
47
+ response = response.split("Assistant:")[-1].strip()
48
+ response_lines = response.split('\n')
49
+ clean_response = []
50
+ for line in response_lines:
51
+ if "User:" not in line and "Assistant:" not in line:
52
+ clean_response.append(line)
53
+ response = ' '.join(clean_response)
54
+
55
+ return [(message, response)]
56
 
57
+ # Create the Gradio chat interface
58
+ demo = gr.ChatInterface(
59
+ fn=generate_response,
60
+ additional_inputs=[
61
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
62
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
63
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
64
+ gr.Slider(
65
+ minimum=0.1,
66
+ maximum=1.0,
67
+ value=0.95,
68
+ step=0.05,
69
+ label="Top-p (nucleus sampling)",
70
+ ),
71
  ],
72
+ title="Chatbot",
73
+ description="Ask anything to the chatbot."
74
  )
75
 
76
+ if __name__ == "__main__":
77
+ demo.launch()