Yaswanth sai commited on
Commit
aeb0977
·
1 Parent(s): 7e93258

Updated Space with Gradio interface and configuration

Browse files
Files changed (2) hide show
  1. app.py +46 -50
  2. requirements.txt +1 -1
app.py CHANGED
@@ -2,48 +2,53 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
4
  import torch
 
5
 
6
  # Constants
7
  MODEL_NAME = "Salesforce/codegen-350M-mono"
8
  LORA_PATH = "fine-tuned-model"
9
 
10
  # Initialize tokenizer and model
 
11
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
 
 
12
  base_model = AutoModelForCausalLM.from_pretrained(
13
  MODEL_NAME,
14
- torch_dtype=torch.float16,
15
  device_map="auto",
16
- trust_remote_code=True
17
  )
18
 
19
- # Load the fine-tuned model
20
  model = PeftModel.from_pretrained(
21
  base_model,
22
  LORA_PATH,
23
- torch_dtype=torch.float16,
24
- device_map="auto"
25
  )
26
 
27
  def generate_response(task_description, code_snippet, request_type, mode="concise"):
28
- # Format the prompt based on request type
29
- if request_type == "hint":
30
- prompt = f"""Task Description: {task_description}
 
31
 
32
  User's Code:
33
  {code_snippet}
34
 
35
  AI-HR Assistant: Here's a hint to help you:
36
  HINT:"""
37
- elif request_type == "feedback":
38
- prompt = f"""Task Description: {task_description}
39
 
40
  User's Code:
41
  {code_snippet}
42
 
43
  AI-HR Assistant: Here's my feedback on your code:
44
  FEEDBACK:"""
45
- else: # follow-up
46
- prompt = f"""Task Description: {task_description}
47
 
48
  User's Code:
49
  {code_snippet}
@@ -51,28 +56,30 @@ User's Code:
51
  AI-HR Assistant: Here's a follow-up question to extend your learning:
52
  FOLLOW-UP:"""
53
 
54
- # Generate response
55
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
56
- with torch.no_grad():
57
- outputs = model.generate(
58
- **inputs,
59
- max_new_tokens=256 if mode == "detailed" else 128,
60
- do_sample=True,
61
- temperature=0.7,
62
- top_p=0.95,
63
- )
64
-
65
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
66
-
67
- # Extract the relevant part of the response
68
- if request_type == "hint" and "HINT:" in response:
69
- response = response.split("HINT:", 1)[1].strip()
70
- elif request_type == "feedback" and "FEEDBACK:" in response:
71
- response = response.split("FEEDBACK:", 1)[1].strip()
72
- elif request_type == "follow-up" and "FOLLOW-UP:" in response:
73
- response = response.split("FOLLOW-UP:", 1)[1].strip()
74
-
75
- return response
 
 
76
 
77
  # Create Gradio interface
78
  with gr.Blocks(title="Live Coding HR Assistant") as demo:
@@ -83,13 +90,13 @@ with gr.Blocks(title="Live Coding HR Assistant") as demo:
83
  with gr.Column():
84
  task_description = gr.Textbox(
85
  label="Task Description",
86
- placeholder="Enter the coding problem or task description...",
87
  lines=5
88
  )
89
  code_snippet = gr.Code(
90
  label="Code Snippet",
91
  language="python",
92
- placeholder="Enter your code here..."
93
  )
94
  request_type = gr.Radio(
95
  choices=["hint", "feedback", "follow-up"],
@@ -107,7 +114,7 @@ with gr.Blocks(title="Live Coding HR Assistant") as demo:
107
  output = gr.Textbox(
108
  label="AI Response",
109
  lines=8,
110
- placeholder="AI response will appear here..."
111
  )
112
 
113
  submit_btn.click(
@@ -115,18 +122,7 @@ with gr.Blocks(title="Live Coding HR Assistant") as demo:
115
  inputs=[task_description, code_snippet, request_type, mode],
116
  outputs=output
117
  )
118
-
119
- gr.Markdown("""
120
- ### How to use
121
- 1. Enter your coding task description
122
- 2. Paste your code (if any)
123
- 3. Choose what you need:
124
- - **Hint**: Get a helpful hint without the full solution
125
- - **Feedback**: Get constructive feedback on your code
126
- - **Follow-up**: Get a follow-up question to extend your learning
127
- 4. Choose response style (concise or detailed)
128
- 5. Click "Get Response"
129
- """)
130
 
131
  # Launch the app
132
- demo.launch()
 
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
4
  import torch
5
+ import os
6
 
7
  # Constants
8
  MODEL_NAME = "Salesforce/codegen-350M-mono"
9
  LORA_PATH = "fine-tuned-model"
10
 
11
  # Initialize tokenizer and model
12
+ print("Loading tokenizer...")
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
14
+
15
+ print("Loading base model...")
16
  base_model = AutoModelForCausalLM.from_pretrained(
17
  MODEL_NAME,
18
+ trust_remote_code=True,
19
  device_map="auto",
20
+ torch_dtype=torch.float32 # Changed from float16 to float32 for better compatibility
21
  )
22
 
23
+ print("Loading fine-tuned model...")
24
  model = PeftModel.from_pretrained(
25
  base_model,
26
  LORA_PATH,
27
+ device_map="auto",
28
+ torch_dtype=torch.float32
29
  )
30
 
31
  def generate_response(task_description, code_snippet, request_type, mode="concise"):
32
+ try:
33
+ # Format the prompt based on request type
34
+ if request_type == "hint":
35
+ prompt = f"""Task Description: {task_description}
36
 
37
  User's Code:
38
  {code_snippet}
39
 
40
  AI-HR Assistant: Here's a hint to help you:
41
  HINT:"""
42
+ elif request_type == "feedback":
43
+ prompt = f"""Task Description: {task_description}
44
 
45
  User's Code:
46
  {code_snippet}
47
 
48
  AI-HR Assistant: Here's my feedback on your code:
49
  FEEDBACK:"""
50
+ else: # follow-up
51
+ prompt = f"""Task Description: {task_description}
52
 
53
  User's Code:
54
  {code_snippet}
 
56
  AI-HR Assistant: Here's a follow-up question to extend your learning:
57
  FOLLOW-UP:"""
58
 
59
+ # Generate response
60
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
61
+ with torch.no_grad():
62
+ outputs = model.generate(
63
+ **inputs,
64
+ max_new_tokens=256 if mode == "detailed" else 128,
65
+ do_sample=True,
66
+ temperature=0.7,
67
+ top_p=0.95,
68
+ )
69
+
70
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+
72
+ # Extract the relevant part of the response
73
+ if request_type == "hint" and "HINT:" in response:
74
+ response = response.split("HINT:", 1)[1].strip()
75
+ elif request_type == "feedback" and "FEEDBACK:" in response:
76
+ response = response.split("FEEDBACK:", 1)[1].strip()
77
+ elif request_type == "follow-up" and "FOLLOW-UP:" in response:
78
+ response = response.split("FOLLOW-UP:", 1)[1].strip()
79
+
80
+ return response
81
+ except Exception as e:
82
+ return f"An error occurred: {str(e)}"
83
 
84
  # Create Gradio interface
85
  with gr.Blocks(title="Live Coding HR Assistant") as demo:
 
90
  with gr.Column():
91
  task_description = gr.Textbox(
92
  label="Task Description",
93
+ value="",
94
  lines=5
95
  )
96
  code_snippet = gr.Code(
97
  label="Code Snippet",
98
  language="python",
99
+ value=""
100
  )
101
  request_type = gr.Radio(
102
  choices=["hint", "feedback", "follow-up"],
 
114
  output = gr.Textbox(
115
  label="AI Response",
116
  lines=8,
117
+ value=""
118
  )
119
 
120
  submit_btn.click(
 
122
  inputs=[task_description, code_snippet, request_type, mode],
123
  outputs=output
124
  )
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  # Launch the app
127
+ if __name__ == "__main__":
128
+ demo.launch()
requirements.txt CHANGED
@@ -6,5 +6,5 @@ peft
6
  accelerate
7
  sentencepiece
8
  python-multipart
9
- gradio>=4.12.0
10
  bitsandbytes
 
6
  accelerate
7
  sentencepiece
8
  python-multipart
9
+ gradio>=4.44.1
10
  bitsandbytes