sagarkariya commited on
Commit
0f6e3cb
Β·
verified Β·
1 Parent(s): 94cbf31

Created app.py with CodeLlama-3b model

Browse files
Files changed (1) hide show
  1. app.py +69 -0
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.")