anupajose commited on
Commit
107a844
·
verified ·
1 Parent(s): fc03491

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -81
app.py CHANGED
@@ -1,82 +1,124 @@
1
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
- from llama_index.readers.file import PDFReader
4
- from llama_index.core import VectorStoreIndex
5
- from llama_index.llms.openai import OpenAI
6
-
7
-
8
- # Set API key (best practice: use HF secrets in actual deployment)
9
- os.environ['OPENAI_API_KEY'] = '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'
10
-
11
- # Global vars
12
- query_engine = None
13
- interview_questions = []
14
- resume_summary = ""
15
-
16
- # Step 1: Load Resume
17
- def load_resume(file):
18
- global query_engine, interview_questions, resume_summary
19
-
20
- reader = PDFReader()
21
- documents = reader.load_data(file=file.name)
22
-
23
- index = VectorStoreIndex.from_documents(documents)
24
- query_engine = index.as_query_engine()
25
-
26
- # Summarize resume
27
- resume_summary = query_engine.query("Summarize the key points in this resume")
28
-
29
- # Generate interview questions
30
- q_prompt = "Generate 5 interview questions based on this resume:\n" + str(resume_summary)
31
- interview_questions.clear()
32
- for i in range(5):
33
- q = query_engine.query(q_prompt + f"\nQuestion {i+1}")
34
- interview_questions.append(str(q))
35
-
36
- return f"✅ Resume uploaded.\n\n📝 Summary:\n{resume_summary}"
37
-
38
- # Step 2: Show Questions
39
- def show_questions():
40
- if not interview_questions:
41
- return "❌ Please upload and analyze a resume first."
42
- return "\n".join([f"{i+1}. {q}" for i, q in enumerate(interview_questions)])
43
-
44
- # Step 3: Evaluate Answer
45
- def evaluate_answer(answer):
46
- if not answer.strip():
47
- return "⚠️ Please provide an answer."
48
- word_count = len(answer.split())
49
- score = min(word_count // 10, 5)
50
- stars = "⭐" * score
51
- return f"✅ Answer received.\nScore: {stars} ({score}/5)"
52
-
53
- # Step 4: Rate Resume
54
- def rate_resume():
55
- if not resume_summary:
56
- return "❌ Upload a resume first."
57
- rating = query_engine.query("Evaluate and rate the quality of this resume from 1 to 10. Only return the number.")
58
- return f"📊 Resume Rating: {rating}/10"
59
-
60
- # Interface layout
61
- with gr.Blocks() as demo:
62
- gr.Markdown("# 🤖 Resume Interview Bot\nUpload your resume, get interview questions, answer them, and get feedback!")
63
-
64
- with gr.Row():
65
- resume_input = gr.File(label="📄 Upload Resume (.pdf)", file_types=[".pdf"])
66
- resume_status = gr.Textbox(label="Resume Summary", lines=6)
67
- gr.Button("Analyze Resume").click(load_resume, inputs=resume_input, outputs=resume_status)
68
-
69
- gr.Markdown("### 🎯 Generated Interview Questions")
70
- question_box = gr.Textbox(label="Questions", lines=7)
71
- gr.Button("Get Questions").click(show_questions, outputs=question_box)
72
-
73
- gr.Markdown("### 🗣️ Answer a Question")
74
- answer_input = gr.Textbox(label="Your Answer")
75
- answer_result = gr.Textbox(label="Feedback", interactive=False)
76
- gr.Button("Submit Answer").click(evaluate_answer, inputs=answer_input, outputs=answer_result)
77
-
78
- gr.Markdown("### 📈 Resume Quality Rating")
79
- rating_output = gr.Textbox(label="Rating", interactive=False)
80
- gr.Button("Rate Resume").click(rate_resume, outputs=rating_output)
81
-
82
- demo.launch()
 
1
+ import torch
2
+ from datasets import Dataset
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, BitsAndBytesConfig
4
+ from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
5
+
6
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
+
8
+ bnb_config = BitsAndBytesConfig(
9
+ load_in_4bit=True,
10
+ bnb_4bit_compute_dtype=torch.float16,
11
+ bnb_4bit_use_double_quant=True,
12
+ bnb_4bit_quant_type="nf4",
13
+ )
14
+
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ model_name,
17
+ quantization_config=bnb_config,
18
+ device_map="auto"
19
+ )
20
+
21
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
22
+ tokenizer.pad_token = tokenizer.eos_token
23
+ model.config.use_cache = False
24
+ model.gradient_checkpointing_enable()
25
+ model = prepare_model_for_kbit_training(model)
26
+
27
+ lora_config = LoraConfig(
28
+ r=8,
29
+ lora_alpha=32,
30
+ target_modules=["q_proj", "v_proj"],
31
+ lora_dropout=0.05,
32
+ bias="none",
33
+ task_type="CAUSAL_LM"
34
+ )
35
+
36
+ model = get_peft_model(model, lora_config)
37
+ import pandas as pd
38
+ from datasets import Dataset
39
+
40
+
41
+ # Load data from CSV
42
+ df = pd.read_csv("Customer-Support.csv")
43
+
44
+
45
+ # Rename columns to match expected keys
46
+ df = df.rename(columns={"query": "instruction", "response": "output"})
47
+
48
+
49
+ # Select required columns
50
+ data = df[["instruction", "output"]].fillna("")
51
+
52
+
53
+ # Convert DataFrame to list of dictionaries
54
+ data = data.to_dict(orient="records")
55
+
56
+
57
+ # Create Hugging Face Dataset
58
+ dataset = Dataset.from_list(data)
59
+
60
+
61
+ # Format each example
62
+ def format_instruction(example):
63
+ return f"### Instruction:\n{example['instruction']}\n\n### Response:\n{example['output']}"
64
+
65
+
66
+ # Map formatted text
67
+ dataset = dataset.map(lambda x: {"text": format_instruction(x)})
68
+
69
+ def tokenize_function(example):
70
+ tokenized = tokenizer(example["text"], truncation=True, padding="max_length", max_length=512)
71
+ tokenized["labels"] = tokenized["input_ids"].copy()
72
+ return tokenized
73
+
74
+ tokenized_dataset = dataset.map(tokenize_function, batched=True)
75
+ training_args = TrainingArguments(
76
+ output_dir="./tinyllama-qlora-support-bot",
77
+ per_device_train_batch_size=2,
78
+ gradient_accumulation_steps=4,
79
+ learning_rate=2e-4,
80
+ logging_dir="./logs",
81
+ num_train_epochs=3,
82
+ logging_steps=10,
83
+ save_total_limit=2,
84
+ save_strategy="epoch",
85
+ bf16=True,
86
+ optim="paged_adamw_8bit"
87
+ )
88
+
89
+ trainer = Trainer(
90
+ model=model,
91
+ args=training_args,
92
+ train_dataset=tokenized_dataset,
93
+ tokenizer=tokenizer
94
+ )
95
+
96
+ trainer.train()
97
+ model.save_pretrained("tinyllama-qlora-support-bot")
98
+ tokenizer.save_pretrained("tinyllama-qlora-support-bot")
99
+
100
+ from transformers import pipeline
101
+
102
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
103
+
104
+ instruction = "How do I update the app?"
105
+ prompt = f"### Instruction:\n{instruction}\n\n### Response:\n"
106
+
107
+ output = pipe(prompt, max_new_tokens=100)
108
+ print(output[0]['generated_text'])
109
  import gradio as gr
110
+
111
+ def generate_response(instruction):
112
+ prompt = f"### Instruction:\n{instruction}\n\n### Response:\n"
113
+ output = pipe(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)
114
+ # Extract only the response part
115
+ response = output[0]["generated_text"].split("### Response:\n")[-1].strip()
116
+ return response
117
+
118
+ gr.Interface(
119
+ fn=generate_response,
120
+ inputs=gr.Textbox(lines=3, label="Enter your question"),
121
+ outputs=gr.Textbox(lines=5, label="Support Bot's Response"),
122
+ title="📞 Customer Support Chatbot",
123
+ description="Ask a question and get a response from your fine-tuned TinyLLaMA model.",
124
+ ).launch()