File size: 6,098 Bytes
818bad6
 
 
 
 
7a25c1c
cc699e9
 
818bad6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a25c1c
818bad6
 
 
490e084
 
 
 
 
 
 
 
6e449bc
 
 
 
 
818bad6
490e084
818bad6
 
 
 
 
 
 
490e084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
818bad6
490e084
 
818bad6
490e084
 
818bad6
490e084
 
818bad6
490e084
 
818bad6
490e084
 
 
818bad6
490e084
7a25c1c
490e084
 
818bad6
490e084
 
 
818bad6
490e084
 
818bad6
490e084
 
 
818bad6
490e084
 
 
 
 
818bad6
490e084
 
818bad6
490e084
 
 
818bad6
490e084
 
833bbde
490e084
 
 
 
 
 
 
 
 
 
6e449bc
 
 
 
 
 
490e084
 
6e449bc
 
 
 
 
 
 
 
 
 
 
 
490e084
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import streamlit as st
import PyPDF2
import openai
import faiss
import os
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_file):
    reader = PyPDF2.PdfReader(pdf_file)
    text = ""
    for page in reader.pages:
        text += page.extract_text()
    return text

# Function to generate embeddings for a piece of text
def get_embeddings(text, model="text-embedding-ada-002"):
    response = openai.Embedding.create(input=[text], model=model)
    return response['data'][0]['embedding']

# Function to search for similar content
def search_similar(query_embedding, index, stored_texts, top_k=3):
    distances, indices = index.search(np.array([query_embedding]), top_k)
    results = [(stored_texts[i], distances[0][idx]) for idx, i in enumerate(indices[0])]
    return results

# Function to generate code based on a prompt
def generate_code_from_prompt(prompt, model="gpt-4o-mini"):
    response = openai.ChatCompletion.create(
        model=model,
        messages=[{"role": "user", "content": prompt}]
    )
    return response['choices'][0]['message']['content']

# Function to save code to a .txt file
def save_code_to_file(code, filename="generated_code.txt"):
    with open(filename, "w") as f:
        f.write(code)

# Streamlit app starts here
st.title("AI Assistance")

# Input OpenAI API key
openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")

if openai_api_key:
    openai.api_key = openai_api_key

    # Sidebar to toggle between Course Query Assistant and Code Generator
    st.sidebar.title("Select Mode")
    mode = st.sidebar.radio("Choose an option", ("Course Query Assistant", "Code Generator"))

    if mode == "Course Query Assistant":
        st.header("Course Query Assistant")

        # Upload course materials
        uploaded_files = st.file_uploader("Upload Course Materials (PDFs)", type=["pdf"], accept_multiple_files=True)

        if uploaded_files:
            st.write("Processing uploaded course materials...")

            # Extract text and generate embeddings for all uploaded PDFs
            course_texts = []
            for uploaded_file in uploaded_files:
                text = extract_text_from_pdf(uploaded_file)
                course_texts.append(text)

            # Combine all course materials into one large text
            combined_text = " ".join(course_texts)

            # Split combined text into smaller chunks for embedding (max tokens ~1000)
            chunks = [combined_text[i:i+1000] for i in range(0, len(combined_text), 1000)]

            # Generate embeddings for all chunks
            embeddings = [get_embeddings(chunk) for chunk in chunks]

            # Convert the list of embeddings into a NumPy array (shape: [num_chunks, embedding_size])
            embeddings_np = np.array(embeddings).astype("float32")

            # Create a FAISS index for similarity search
            index = faiss.IndexFlatL2(len(embeddings_np[0]))  # Use the length of the embedding vectors for the dimension
            index.add(embeddings_np)

            st.write("Course materials have been processed and indexed.")

            # User query
            query = st.text_input("Enter your question about the course materials:")

            if query:
                # Generate embedding for the query
                query_embedding = get_embeddings(query)

                # Search for similar chunks in the FAISS index
                results = search_similar(query_embedding, index, chunks)

                # Create the context for the GPT prompt
                context = "\n".join([result[0] for result in results])
                modified_prompt = f"Context: {context}\n\nQuestion: {query}\n\nProvide a detailed answer based on the context."

                # Get the GPT-4 response
                response = openai.ChatCompletion.create(
                    model="gpt-4o-mini",  # Update to GPT-4 (or your desired model)
                    messages=[{"role": "user", "content": modified_prompt}]
                )

                # Get the response content
                response_content = response['choices'][0]['message']['content']

                # Display the response in Streamlit (Intelligent Reply)
                st.write("### Intelligent Reply:")
                st.write(response_content)

    elif mode == "Code Generator":
        st.header("Code Generator")

        # Code generation prompt input
        code_prompt = st.text_area("Describe the code you want to generate:", 
                                   "e.g., Write a Python program that generates Fibonacci numbers.")
        
        if st.button("Generate Code"):
            if code_prompt:
                with st.spinner("Generating code..."):
                    # Generate code using GPT-4
                    generated_code = generate_code_from_prompt(code_prompt)
                    
                    # Clean the generated code to ensure only code is saved (removing comments or additional text)
                    clean_code = "\n".join([line for line in generated_code.splitlines() if not line.strip().startswith("#")])

                    # Save the clean code to a file
                    save_code_to_file(clean_code)

                    # Display the generated code
                    st.write("### Generated Code:")
                    st.code(clean_code, language="python")

                    # Provide a download link for the generated code
                    with open("generated_code.txt", "w") as f:
                        f.write(clean_code)

                    st.download_button(
                        label="Download Generated Code",
                        data=open("generated_code.txt", "rb").read(),
                        file_name="generated_code.txt",
                        mime="text/plain"
                    )
            else:
                st.error("Please provide a prompt to generate the code.")