Spaces:
Sleeping
Sleeping
Created app.py with CodeLlama-3b model
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
import faiss
|
5 |
+
import numpy as np
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
|
9 |
+
# π― Load CodeLlama 3B Model
|
10 |
+
model_name = "codellama/CodeLlama-3b"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
13 |
+
|
14 |
+
# π Function to Generate Responses
|
15 |
+
def generate_response(prompt):
|
16 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
17 |
+
output = model.generate(**inputs, max_length=300)
|
18 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
# π FAISS Index Creation
|
21 |
+
def create_faiss_index(texts):
|
22 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
23 |
+
embedding_vectors = embeddings.embed_documents(texts)
|
24 |
+
faiss_index = faiss.IndexFlatL2(len(embedding_vectors[0]))
|
25 |
+
faiss_index.add(np.array(embedding_vectors))
|
26 |
+
return faiss_index
|
27 |
+
|
28 |
+
# π Function to Parse Logs
|
29 |
+
def parse_test_log(file):
|
30 |
+
try:
|
31 |
+
log_content = file.read().decode("utf-8")
|
32 |
+
return log_content.splitlines() # Split logs into lines
|
33 |
+
except Exception as e:
|
34 |
+
st.error(f"Error reading file: {e}")
|
35 |
+
return []
|
36 |
+
|
37 |
+
# π Streamlit UI
|
38 |
+
st.title("π Test Log Failure Analysis with CodeLlama 3B")
|
39 |
+
|
40 |
+
# π File Upload
|
41 |
+
uploaded_file = st.file_uploader("Upload test log (txt, json, xml, html)", type=["txt", "json", "xml", "html"])
|
42 |
+
|
43 |
+
if uploaded_file is not None:
|
44 |
+
st.info("Processing the test log file...")
|
45 |
+
test_log_lines = parse_test_log(uploaded_file)
|
46 |
+
|
47 |
+
# π Create FAISS Index
|
48 |
+
st.info("Indexing log data...")
|
49 |
+
faiss_index = create_faiss_index(test_log_lines)
|
50 |
+
|
51 |
+
# β User Query
|
52 |
+
question = st.text_input("Ask a question about the test failures:")
|
53 |
+
|
54 |
+
if question:
|
55 |
+
# π Retrieve Similar Logs from FAISS
|
56 |
+
query_embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2").embed_query(question)
|
57 |
+
_, I = faiss_index.search(np.array([query_embedding]), k=5) # Retrieve top 5 results
|
58 |
+
|
59 |
+
# π Combine the most relevant logs
|
60 |
+
context = "\n".join([test_log_lines[i] for i in I[0]])
|
61 |
+
|
62 |
+
# π€ Generate Answer with CodeLlama
|
63 |
+
prompt = f"Given the following test logs:\n{context}\n\nAnswer: {question}"
|
64 |
+
answer = generate_response(prompt)
|
65 |
+
st.subheader("Analysis Result:")
|
66 |
+
st.write(answer)
|
67 |
+
|
68 |
+
else:
|
69 |
+
st.info("Upload a test log file to begin analysis.")
|