import gradio as gr import os import torch import numpy as np import random from huggingface_hub import login, HfFolder from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM, TextIteratorStreamer from scipy.special import softmax import logging import spaces from threading import Thread from collections.abc import Iterator import csv # Increase CSV field size limit csv.field_size_limit(1000000) # Setup logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') # Set a seed for reproducibility seed = 42 np.random.seed(seed) random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed) # Login to Hugging Face token = os.getenv("hf_token") HfFolder.save_token(token) login(token) # --- Quality Prediction Model Setup --- model_paths = [ 'karths/binary_classification_train_test', "karths/binary_classification_train_process", "karths/binary_classification_train_infrastructure", "karths/binary_classification_train_documentation", "karths/binary_classification_train_design", "karths/binary_classification_train_defect", "karths/binary_classification_train_code", "karths/binary_classification_train_build", "karths/binary_classification_train_automation", "karths/binary_classification_train_people", "karths/binary_classification_train_architecture", ] quality_mapping = { 'binary_classification_train_test': 'Test', 'binary_classification_train_process': 'Process', 'binary_classification_train_infrastructure': 'Infrastructure', 'binary_classification_train_documentation': 'Documentation', 'binary_classification_train_design': 'Design', 'binary_classification_train_defect': 'Defect', 'binary_classification_train_code': 'Code', 'binary_classification_train_build': 'Build', 'binary_classification_train_automation': 'Automation', 'binary_classification_train_people': 'People', 'binary_classification_train_architecture': 'Architecture' } # Pre-load models and tokenizer for quality prediction tokenizer = AutoTokenizer.from_pretrained("distilroberta-base") models = {path: AutoModelForSequenceClassification.from_pretrained(path) for path in model_paths} # Load to CPU initially def get_quality_name(model_name): return quality_mapping.get(model_name.split('/')[-1], "Unknown Quality") def model_prediction(model, text, device): model.to(device) # Move the *specific* model to the GPU model.eval() inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) inputs = {k: v.to(device) for k, v in inputs.items()} with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits probs = softmax(logits.cpu().numpy(), axis=1) avg_prob = np.mean(probs[:, 1]) model.to("cpu") # Move the model *back* to the CPU return avg_prob # --- Llama 3.2 3B Model Setup --- LLAMA_MAX_MAX_NEW_TOKENS = 2048 LLAMA_DEFAULT_MAX_NEW_TOKENS = 512 # Reduced for efficiency LLAMA_MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "2048")) # Reduced llama_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Explicit device llama_model_id = "meta-llama/Llama-3.2-3B-Instruct" llama_tokenizer = AutoTokenizer.from_pretrained(llama_model_id) llama_model = AutoModelForCausalLM.from_pretrained( llama_model_id, device_map="auto", # Let Transformers handle optimal device placement torch_dtype=torch.bfloat16, ) llama_model.eval() if llama_tokenizer.pad_token is None: llama_tokenizer.pad_token = llama_tokenizer.eos_token def llama_generate( message: str, max_new_tokens: int = LLAMA_DEFAULT_MAX_NEW_TOKENS, temperature: float = 0.6, top_p: float = 0.9, top_k: int = 50, repetition_penalty: float = 1.2, ) -> Iterator[str]: inputs = llama_tokenizer(message, return_tensors="pt", padding=True, truncation=True, max_length=LLAMA_MAX_INPUT_TOKEN_LENGTH).to(llama_model.device) if inputs.input_ids.shape[1] > LLAMA_MAX_INPUT_TOKEN_LENGTH: inputs.input_ids = inputs.input_ids[:, -LLAMA_MAX_INPUT_TOKEN_LENGTH:] gr.Warning(f"Trimmed input from conversation as it was longer than {LLAMA_MAX_INPUT_TOKEN_LENGTH} tokens.") streamer = TextIteratorStreamer(llama_tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True) generate_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_new_tokens, do_sample=True, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=1, repetition_penalty=repetition_penalty, ) t = Thread(target=llama_model.generate, kwargs=generate_kwargs) t.start() outputs = [] for text in streamer: outputs.append(text) yield "".join(outputs) torch.cuda.empty_cache() # Clear cache after each generation def generate_explanation(issue_text, top_qualities): """Generates an explanation using Llama 3.2 3B.""" if not top_qualities: return "No explanation available as no quality tags were predicted." prompt = f""" Given the following issue description: --- {issue_text} --- Explain why this issue might be classified under the following quality categories: {', '.join([q[0] for q in top_qualities])}. Provide a concise explanation for each category, relating it back to the issue description. """ explanation = "" try: for chunk in llama_generate(prompt): explanation += chunk except Exception as e: logging.error(f"Error during Llama generation: {e}") return "An error occurred while generating the explanation." return explanation # @spaces.GPU(duration=180) # Apply the GPU decorator *only* to the main interface def main_interface(text): if not text.strip(): return "