Spaces:
No application file
No application file
Yaswanth sai
commited on
Commit
·
d0e8138
1
Parent(s):
f9ab4db
app.py
DELETED
@@ -1,118 +0,0 @@
|
|
1 |
-
import gradio as gr
|
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.float16
|
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.float16
|
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: {task_description}\nCode:\n{code_snippet}\nHINT:"
|
36 |
-
elif request_type == "feedback":
|
37 |
-
prompt = f"Task: {task_description}\nCode:\n{code_snippet}\nFEEDBACK:"
|
38 |
-
else: # follow-up
|
39 |
-
prompt = f"Task: {task_description}\nCode:\n{code_snippet}\nFOLLOW-UP:"
|
40 |
-
|
41 |
-
# Encode and generate
|
42 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
43 |
-
|
44 |
-
outputs = model.generate(
|
45 |
-
**inputs,
|
46 |
-
max_new_tokens=256 if mode == "detailed" else 128,
|
47 |
-
do_sample=True,
|
48 |
-
temperature=0.7,
|
49 |
-
top_p=0.95,
|
50 |
-
)
|
51 |
-
|
52 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
53 |
-
|
54 |
-
# Extract the relevant part of the response
|
55 |
-
if request_type == "hint" and "HINT:" in response:
|
56 |
-
response = response.split("HINT:", 1)[1].strip()
|
57 |
-
elif request_type == "feedback" and "FEEDBACK:" in response:
|
58 |
-
response = response.split("FEEDBACK:", 1)[1].strip()
|
59 |
-
elif request_type == "follow-up" and "FOLLOW-UP:" in response:
|
60 |
-
response = response.split("FOLLOW-UP:", 1)[1].strip()
|
61 |
-
|
62 |
-
return response
|
63 |
-
except Exception as e:
|
64 |
-
return f"An error occurred: {str(e)}"
|
65 |
-
|
66 |
-
# Create Gradio interface
|
67 |
-
with gr.Blocks(title="Live Coding HR Assistant") as demo:
|
68 |
-
gr.Markdown("# 💻 Live Coding HR Assistant")
|
69 |
-
gr.Markdown("Get hints, feedback, and follow-up questions for your coding tasks!")
|
70 |
-
|
71 |
-
with gr.Row():
|
72 |
-
with gr.Column():
|
73 |
-
task_description = gr.Textbox(
|
74 |
-
label="Task Description",
|
75 |
-
value="",
|
76 |
-
lines=5
|
77 |
-
)
|
78 |
-
code_snippet = gr.Code(
|
79 |
-
label="Code Snippet",
|
80 |
-
language="python",
|
81 |
-
value=""
|
82 |
-
)
|
83 |
-
request_type = gr.Radio(
|
84 |
-
choices=["hint", "feedback", "follow-up"],
|
85 |
-
label="What would you like?",
|
86 |
-
value="hint"
|
87 |
-
)
|
88 |
-
mode = gr.Radio(
|
89 |
-
choices=["concise", "detailed"],
|
90 |
-
label="Response Style",
|
91 |
-
value="concise"
|
92 |
-
)
|
93 |
-
submit_btn = gr.Button("Get Response", variant="primary")
|
94 |
-
|
95 |
-
with gr.Column():
|
96 |
-
output = gr.Textbox(
|
97 |
-
label="AI Response",
|
98 |
-
lines=8,
|
99 |
-
value=""
|
100 |
-
)
|
101 |
-
|
102 |
-
submit_btn.click(
|
103 |
-
fn=generate_response,
|
104 |
-
inputs=[task_description, code_snippet, request_type, mode],
|
105 |
-
outputs=output,
|
106 |
-
api_name="predict",
|
107 |
-
concurrency_limit=1 # Set concurrency limit here
|
108 |
-
)
|
109 |
-
|
110 |
-
# Configure queue and launch
|
111 |
-
demo.queue(
|
112 |
-
max_size=10 # Removed concurrency_count
|
113 |
-
).launch(
|
114 |
-
server_name="0.0.0.0",
|
115 |
-
server_port=7860,
|
116 |
-
share=True
|
117 |
-
# You can optionally add max_threads here if needed, e.g., max_threads=10
|
118 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|