shukdevdatta123 commited on
Commit
490e084
·
verified ·
1 Parent(s): 262d476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -42
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("Course Query Assistant")
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
- # Upload course materials
39
- uploaded_files = st.file_uploader("Upload Course Materials (PDFs)", type=["pdf"], accept_multiple_files=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- if uploaded_files:
42
- st.write("Processing uploaded course materials...")
43
 
44
- # Extract text and generate embeddings for all uploaded PDFs
45
- course_texts = []
46
- for uploaded_file in uploaded_files:
47
- text = extract_text_from_pdf(uploaded_file)
48
- course_texts.append(text)
49
 
50
- # Combine all course materials into one large text
51
- combined_text = " ".join(course_texts)
52
 
53
- # Split combined text into smaller chunks for embedding (max tokens ~1000)
54
- chunks = [combined_text[i:i+1000] for i in range(0, len(combined_text), 1000)]
55
 
56
- # Generate embeddings for all chunks
57
- embeddings = [get_embeddings(chunk) for chunk in chunks]
 
58
 
59
- # Convert the list of embeddings into a NumPy array (shape: [num_chunks, embedding_size])
60
- embeddings_np = np.array(embeddings).astype("float32")
61
 
62
- # Create a FAISS index for similarity search
63
- index = faiss.IndexFlatL2(len(embeddings_np[0])) # Use the length of the embedding vectors for the dimension
64
- index.add(embeddings_np)
65
 
66
- st.write("Course materials have been processed and indexed.")
 
 
67
 
68
- # User query
69
- query = st.text_input("Enter your question about the course materials:")
70
 
71
- if query:
72
- # Generate embedding for the query
73
- query_embedding = get_embeddings(query)
74
 
75
- # Search for similar chunks in the FAISS index
76
- results = search_similar(query_embedding, index, chunks)
 
 
 
77
 
78
- # Create the context for the GPT prompt
79
- context = "\n".join([result[0] for result in results])
80
- modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."
81
 
82
- # Get the GPT-4 response
83
- response = openai.ChatCompletion.create(
84
- model="gpt-4o-mini", # Update to GPT-4 (or your desired model)
85
- messages=[{"role": "user", "content": modified_prompt}]
86
- )
87
 
88
- # Get the response content
89
- response_content = response['choices'][0]['message']['content']
90
 
91
- # Display the response in Streamlit (Intelligent Reply)
92
- st.write("### Intelligent Reply:")
93
- st.write(response_content)
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")