Manojajj commited on
Commit
8a4c070
·
verified ·
1 Parent(s): 8665d4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -44
app.py CHANGED
@@ -1,57 +1,33 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Load the model and tokenizer
6
- model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
7
- model = AutoModelForCausalLM.from_pretrained(
8
- model_name,
9
- torch_dtype="auto",
10
- device_map="auto"
11
- )
12
- tokenizer = AutoTokenizer.from_pretrained(model_name)
13
 
14
- # Function to interact with the model
15
- def chat_with_model(user_input):
16
- prompt = user_input
17
- messages = [
18
- {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
19
- {"role": "user", "content": prompt}
20
- ]
21
-
22
- # Use apply_chat_template to format messages for the model
23
- text = tokenizer.apply_chat_template(
24
- messages,
25
- tokenize=False,
26
- add_generation_prompt=True
27
- )
28
-
29
- # Tokenize the input and send it to the model
30
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
31
 
32
- # Generate the response from the model
33
- generated_ids = model.generate(
34
- **model_inputs,
35
- max_new_tokens=512
36
- )
37
 
38
- # Decode the generated response
39
- generated_ids = [
40
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
41
- ]
42
 
43
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
44
 
45
- return response
46
 
47
- # Create the Gradio interface
48
  iface = gr.Interface(
49
- fn=chat_with_model,
50
- inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
51
  outputs="text",
52
  title="Qwen2.5-Coder Chatbot",
53
- description="A chatbot using Qwen2.5-Coder for code generation, reasoning, and fixing tasks."
54
  )
55
 
56
- # Launch the interface
57
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Initialize the pipeline for code generation and assistance
5
+ pipe = pipeline("text-generation", model="Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
 
 
 
6
 
7
+ # Function to interact with the model for code-related assistance
8
+ def code_assistance(user_input):
9
+ # Define the system message to set the context
10
+ system_message = "You are Qwen, a code assistant created by Alibaba Cloud. You assist with code generation, debugging, and explanation tasks."
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Format the prompt with the system message and user input (code-related query)
13
+ prompt = f"{system_message}\nUser: {user_input}\nAssistant (Code Assistance):"
 
 
 
14
 
15
+ # Use the pipeline to generate the response for code assistance
16
+ response = pipe(prompt, max_length=512, num_return_sequences=1)
 
 
17
 
18
+ # Extract and clean the response to return only the assistant's code suggestion or explanation
19
+ generated_response = response[0]['generated_text'].split("Assistant (Code Assistance):")[1].strip()
20
 
21
+ return generated_response
22
 
23
+ # Create the Gradio interface for the code assistance chatbot
24
  iface = gr.Interface(
25
+ fn=code_assistance,
26
+ inputs=gr.Textbox(lines=5, placeholder="Ask for code help..."),
27
  outputs="text",
28
  title="Qwen2.5-Coder Chatbot",
29
+ description="A chatbot using Qwen2.5-Coder for code generation, debugging, and explanation tasks."
30
  )
31
 
32
+ # Launch the Gradio interface
33
+ iface.launch()