Skip to content

Commit

Permalink
mindmap iter 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudhendra committed Oct 22, 2024
1 parent e7e0c21 commit fc8171f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 63 deletions.
19 changes: 5 additions & 14 deletions medapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,29 +282,21 @@ def mindmap_tab_content(video_data):
with st.spinner("Generating Mindmap..."):
try:
# Generate Mindmap
mindmap_html, mindmap_analysis = get_mindmap_data(
mindmap_markdown = get_mindmap_data(
st.session_state.user_query,
st.session_state.relevant_passages,
st.session_state.answer,
video_data
st.session_state.answer
)
st.session_state.mindmap_html = mindmap_html
st.session_state.mindmap_analysis = mindmap_analysis
st.session_state.mindmap_markdown = mindmap_markdown

st.success("Mindmap generated successfully!")
logging.info("Mindmap generated successfully")
except Exception as e:
st.error(f"Error generating mindmap: {str(e)}")
logging.error(f"Error generating mindmap: {str(e)}", exc_info=True)

if st.session_state.get('mindmap_html'):
st.components.v1.html(st.session_state.mindmap_html, height=600)

st.subheader("Mindmap Analysis")
if st.session_state.mindmap_analysis:
st.markdown(st.session_state.mindmap_analysis)
else:
st.info("No mindmap analysis available yet.")
if st.session_state.get('mindmap_markdown'):
st.markdown(st.session_state.mindmap_markdown)
else:
st.info("Generate a mindmap by submitting a query in the Main tab and then clicking the 'Generate Mindmap' button above.")

Expand All @@ -323,4 +315,3 @@ def mermaid_to_html(mermaid_code):

if __name__ == "__main__":
main()

57 changes: 8 additions & 49 deletions mindmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pyvis.network import Network

def extract_key_terms(relevant_passages):
# Extract key terms from the relevant passages
context = " ".join([p['text'] for p in relevant_passages])
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
response = client.chat.completions.create(
Expand All @@ -30,68 +29,29 @@ def extract_key_terms(relevant_passages):
key_terms = response.choices[0].message.content.strip().split(',')
return [term.strip() for term in key_terms if term.strip()]

def generate_mindmap(query, relevant_passages, answer, all_data):
def generate_mindmap(query, relevant_passages, answer):
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

# Extract key terms from relevant passages
key_terms = extract_key_terms(relevant_passages)

# Prepare the context for OpenAI
key_terms_text = "\n".join([f"- {term}" for term in key_terms])

# Generate mindmap content using OpenAI
response = client.chat.completions.create(
model="gpt-4o-mini",
model="gpt-4",
messages=[
{
"role": "system",
"content": (
"Create a detailed and accurate mindmap structure based on the given query and key terms. "
"Use Markdown format with # for main topics, ## for subtopics, and ### for further details. "
"Ensure the content is grounded in the provided key terms and aligns with the USMLE Step 1 syllabus."
)
"content": "Create a detailed and accurate mindmap structure based on the given query and key terms. Use Markdown format with # for main topics, ## for subtopics, and ### for further details."
},
{
"role": "user",
"content": (
f"Query: {query}\n\nKey Terms:\n{key_terms_text}\n\n"
f"Answer: {answer}\n\n"
"Create a detailed mindmap structure in Markdown format, ensuring it is comprehensive, informative, and grounded in the key terms:"
)
"content": f"Query: {query}\n\nKey Terms:\n{key_terms_text}\n\nAnswer: {answer}\n\nCreate a detailed mindmap structure in Markdown format:"
}
],
max_tokens=800,
temperature=0.7,
)

mindmap_structure = response.choices[0].message.content.strip()

# Generate analysis and summary
analysis_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": (
"Provide a detailed analysis of how the query relates to the key terms and other topics in the USMLE Step 1 syllabus. "
"Focus on the interconnections and ensure the analysis is accurate and informative."
)
},
{
"role": "user",
"content": (
f"Query: {query}\n\nMindmap Structure:\n{mindmap_structure}\n\n"
"Provide a detailed analysis highlighting the connections between topics, ensuring minimal fluff and maximum informational value:"
)
}
],
max_tokens=800,
temperature=0.7,
)

analysis = analysis_response.choices[0].message.content.strip()

return mindmap_structure, analysis
return mindmap_structure

def create_mindmap(mindmap_structure):
G = nx.DiGraph()
Expand Down Expand Up @@ -183,7 +143,6 @@ def create_mindmap(mindmap_structure):

return content

def get_mindmap_data(query, relevant_passages, answer, all_data):
mindmap_structure, analysis = generate_mindmap(query, relevant_passages, answer, all_data)
mindmap_html = create_mindmap(mindmap_structure)
return mindmap_html, analysis
def get_mindmap_data(query, relevant_passages, answer):
mindmap_structure = generate_mindmap(query, relevant_passages, answer)
return mindmap_structure

0 comments on commit fc8171f

Please sign in to comment.