Update app.py
Browse files
app.py
CHANGED
@@ -26,8 +26,16 @@ def search_similar(query_embedding, index, stored_texts, top_k=3):
|
|
26 |
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
|
27 |
return results
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Streamlit app starts here
|
30 |
-
st.title("
|
31 |
|
32 |
# Input OpenAI API key
|
33 |
openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
@@ -35,59 +43,85 @@ openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
|
35 |
if openai_api_key:
|
36 |
openai.api_key = openai_api_key
|
37 |
|
38 |
-
#
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
for uploaded_file in uploaded_files:
|
47 |
-
text = extract_text_from_pdf(uploaded_file)
|
48 |
-
course_texts.append(text)
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
-
|
60 |
-
embeddings_np = np.array(embeddings).astype("float32")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
index.add(embeddings_np)
|
65 |
|
66 |
-
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
messages=[{"role": "user", "content": modified_prompt}]
|
86 |
-
)
|
87 |
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
|
27 |
return results
|
28 |
|
29 |
+
# Function to generate code based on a prompt
|
30 |
+
def generate_code_from_prompt(prompt, model="gpt-4o-mini"):
|
31 |
+
response = openai.ChatCompletion.create(
|
32 |
+
model=model,
|
33 |
+
messages=[{"role": "user", "content": prompt}]
|
34 |
+
)
|
35 |
+
return response['choices'][0]['message']['content']
|
36 |
+
|
37 |
# Streamlit app starts here
|
38 |
+
st.title("AI Assistance")
|
39 |
|
40 |
# Input OpenAI API key
|
41 |
openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
|
|
43 |
if openai_api_key:
|
44 |
openai.api_key = openai_api_key
|
45 |
|
46 |
+
# Sidebar to toggle between Course Query Assistant and Code Generator
|
47 |
+
st.sidebar.title("Select Mode")
|
48 |
+
mode = st.sidebar.radio("Choose an option", ("Course Query Assistant", "Code Generator"))
|
49 |
+
|
50 |
+
if mode == "Course Query Assistant":
|
51 |
+
st.header("Course Query Assistant")
|
52 |
+
|
53 |
+
# Upload course materials
|
54 |
+
uploaded_files = st.file_uploader("Upload Course Materials (PDFs)", type=["pdf"], accept_multiple_files=True)
|
55 |
+
|
56 |
+
if uploaded_files:
|
57 |
+
st.write("Processing uploaded course materials...")
|
58 |
+
|
59 |
+
# Extract text and generate embeddings for all uploaded PDFs
|
60 |
+
course_texts = []
|
61 |
+
for uploaded_file in uploaded_files:
|
62 |
+
text = extract_text_from_pdf(uploaded_file)
|
63 |
+
course_texts.append(text)
|
64 |
|
65 |
+
# Combine all course materials into one large text
|
66 |
+
combined_text = " ".join(course_texts)
|
67 |
|
68 |
+
# Split combined text into smaller chunks for embedding (max tokens ~1000)
|
69 |
+
chunks = [combined_text[i:i+1000] for i in range(0, len(combined_text), 1000)]
|
|
|
|
|
|
|
70 |
|
71 |
+
# Generate embeddings for all chunks
|
72 |
+
embeddings = [get_embeddings(chunk) for chunk in chunks]
|
73 |
|
74 |
+
# Convert the list of embeddings into a NumPy array (shape: [num_chunks, embedding_size])
|
75 |
+
embeddings_np = np.array(embeddings).astype("float32")
|
76 |
|
77 |
+
# Create a FAISS index for similarity search
|
78 |
+
index = faiss.IndexFlatL2(len(embeddings_np[0])) # Use the length of the embedding vectors for the dimension
|
79 |
+
index.add(embeddings_np)
|
80 |
|
81 |
+
st.write("Course materials have been processed and indexed.")
|
|
|
82 |
|
83 |
+
# User query
|
84 |
+
query = st.text_input("Enter your question about the course materials:")
|
|
|
85 |
|
86 |
+
if query:
|
87 |
+
# Generate embedding for the query
|
88 |
+
query_embedding = get_embeddings(query)
|
89 |
|
90 |
+
# Search for similar chunks in the FAISS index
|
91 |
+
results = search_similar(query_embedding, index, chunks)
|
92 |
|
93 |
+
# Create the context for the GPT prompt
|
94 |
+
context = "\n".join([result[0] for result in results])
|
95 |
+
modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
|
96 |
|
97 |
+
# Get the GPT-4 response
|
98 |
+
response = openai.ChatCompletion.create(
|
99 |
+
model="gpt-4o-mini", # Update to GPT-4 (or your desired model)
|
100 |
+
messages=[{"role": "user", "content": modified_prompt}]
|
101 |
+
)
|
102 |
|
103 |
+
# Get the response content
|
104 |
+
response_content = response['choices'][0]['message']['content']
|
|
|
105 |
|
106 |
+
# Display the response in Streamlit (Intelligent Reply)
|
107 |
+
st.write("### Intelligent Reply:")
|
108 |
+
st.write(response_content)
|
|
|
|
|
109 |
|
110 |
+
elif mode == "Code Generator":
|
111 |
+
st.header("Code Generator")
|
112 |
|
113 |
+
# Code generation prompt input
|
114 |
+
code_prompt = st.text_area("Describe the code you want to generate:",
|
115 |
+
"e.g., Write a Python program that generates Fibonacci numbers.")
|
116 |
+
|
117 |
+
if st.button("Generate Code"):
|
118 |
+
if code_prompt:
|
119 |
+
with st.spinner("Generating code..."):
|
120 |
+
# Generate code using GPT-4
|
121 |
+
generated_code = generate_code_from_prompt(code_prompt)
|
122 |
+
|
123 |
+
# Display the generated code
|
124 |
+
st.write("### Generated Code:")
|
125 |
+
st.code(generated_code, language="python")
|
126 |
+
else:
|
127 |
+
st.error("Please provide a prompt to generate the code.")
|