File size: 2,208 Bytes
e107ee4 |
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 |
import streamlit as st
import datetime
from db import courses_collection2, faculty_collection, students_collection, vectors_collection, chat_history_collection
from PIL import Image
from dotenv import load_dotenv
import os
from datetime import datetime
from bson import ObjectId
from file_upload_vectorize import model
from gen_mcqs import generate_mcqs, quizzes_collection
load_dotenv()
MONGO_URI = os.getenv('MONGO_URI')
OPENAI_KEY = os.getenv('OPENAI_KEY')
GEMINI_KEY = os.getenv('GEMINI_KEY')
def insert_chat_message(user_id, session_id, role, content):
message = {
"role": role,
"content": content,
"timestamp": datetime.utcnow()
}
chat_history_collection.update_one(
{"user_id": ObjectId(user_id), "session_id": session_id},
{"$push": {"messages": message}, "$set": {"timestamp": datetime.utcnow()}},
upsert=True
)
def give_chat_response(user_id, session_id, question, title, description, context):
context_prompt = f"""
Based on the following session title, description, and context, answer the user's question in 3-4 lines:
Title: {title}
Description: {description}
Context: {context}
Question: {question}
Please provide a clear and concise answer based on the information provided.
"""
response = model.generate_content(context_prompt)
if not response or not response.text:
return "No response received from the model"
assistant_response = response.text.strip()
# Save the chat message
insert_chat_message(user_id, session_id, "assistant", assistant_response)
return assistant_response
def create_quiz_by_context(user_id, session_id, context, length, session_title, session_description):
"""Create a quiz based on the context provided"""
quiz = generate_mcqs(context, length, session_title, session_description)
if not quiz:
return "No quiz generated";
# Save the quiz
quizzes_collection.insert_one({
"user_id": ObjectId(user_id),
"session_id": ObjectId(session_id),
"questions": quiz,
"timestamp": datetime.utcnow()
})
return "Quiz created successfully" |