File size: 2,753 Bytes
0f6e3cb
1f17a03
0f6e3cb
 
ed49dce
0f6e3cb
8230759
078b2b8
 
0f6e3cb
783fc68
 
 
 
 
 
1f17a03
 
 
 
 
d0174ef
 
1f17a03
0f6e3cb
 
 
8102ed4
0f6e3cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
import faiss
import os
import numpy as np
from huggingface_hub import login
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS

hf_token = os.getenv("PROJECT_TEST_TOKEN")
if hf_token:
    login(hf_token)
else:
    st.error("Token not found")
    st.stop()


# Load code T5 model 
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-base")
model = AutoModelForSeq2SeqLM.from_pretrained("Salesforce/codet5-base")
device = torch.device("cpu")  
model.to(device)


# πŸ” Function to Generate Responses
def generate_response(prompt):
    inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
    output = model.generate(**inputs, max_length=300)
    return tokenizer.decode(output[0], skip_special_tokens=True)

# πŸ” FAISS Index Creation
def create_faiss_index(texts):
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    embedding_vectors = embeddings.embed_documents(texts)
    faiss_index = faiss.IndexFlatL2(len(embedding_vectors[0]))
    faiss_index.add(np.array(embedding_vectors))
    return faiss_index

# πŸ“‚ Function to Parse Logs
def parse_test_log(file):
    try:
        log_content = file.read().decode("utf-8")
        return log_content.splitlines()  # Split logs into lines
    except Exception as e:
        st.error(f"Error reading file: {e}")
        return []

# 🌟 Streamlit UI
st.title("πŸ” Test Log Failure Analysis with CodeLlama 3B")

# πŸ“‚ File Upload
uploaded_file = st.file_uploader("Upload test log (txt, json, xml, html)", type=["txt", "json", "xml", "html"])

if uploaded_file is not None:
    st.info("Processing the test log file...")
    test_log_lines = parse_test_log(uploaded_file)

    # πŸ” Create FAISS Index
    st.info("Indexing log data...")
    faiss_index = create_faiss_index(test_log_lines)

    # ❓ User Query
    question = st.text_input("Ask a question about the test failures:")

    if question:
        # πŸ” Retrieve Similar Logs from FAISS
        query_embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2").embed_query(question)
        _, I = faiss_index.search(np.array([query_embedding]), k=5)  # Retrieve top 5 results

        # πŸ“ Combine the most relevant logs
        context = "\n".join([test_log_lines[i] for i in I[0]])

        # πŸ€– Generate Answer with CodeLlama
        prompt = f"Given the following test logs:\n{context}\n\nAnswer: {question}"
        answer = generate_response(prompt)
        st.subheader("Analysis Result:")
        st.write(answer)

else:
    st.info("Upload a test log file to begin analysis.")