YAMITEK commited on
Commit
9149aaa
·
verified ·
1 Parent(s): c869b3b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +67 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,69 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
1
+ import os
2
+ from io import BytesIO
3
+
4
  import streamlit as st
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.chains.llm import LLMChain
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ from fpdf import FPDF
9
+
10
+ # 1️⃣ Configure with your API key
11
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyCB5NLx39vOAlfRQBDmnEG3uLBgLraGvH4"
12
+
13
+
14
+ # 2️⃣ Initialize Gemini via LangChain
15
+ model = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0.5)
16
+
17
+ # 3️⃣ Prompt template
18
+ prompt = PromptTemplate.from_template(
19
+ "You are a helpful and motivational AI Mentor. Provide clear, constructive, actionable, and brief guidance (max 100 words) to the following query:\n\n{input}"
20
+ )
21
+ chain = LLMChain(llm=model, prompt=prompt)
22
+
23
+ def ai_mentor(prompt_input: str) -> str:
24
+ return chain.run(input=prompt_input)
25
+
26
+ def create_pdf_buffer(messages) -> BytesIO:
27
+ buffer = BytesIO()
28
+ pdf = FPDF()
29
+ pdf.add_page()
30
+ pdf.set_font("Helvetica", size=14)
31
+ pdf.cell(200, 20, "Chat History with AI Mentor", ln=1, align="C")
32
+ pdf.ln(10)
33
+ pdf.set_font("Helvetica", size=12)
34
+ for msg in messages:
35
+ role = msg["role"].capitalize()
36
+ pdf.multi_cell(0, 8, f"{role}: {msg['content']}")
37
+ pdf.ln(2)
38
+ # Write PDF to memory as bytes
39
+ pdf_bytes = pdf.output(dest="S").encode("latin-1")
40
+ buffer.write(pdf_bytes)
41
+ buffer.seek(0)
42
+ return buffer
43
+
44
+ # ── Streamlit UI ──
45
+ st.title("AI Mentor (Gemini with LangChain)")
46
+ st.sidebar.write("Chat with your AI Mentor. Type questions or worries below 😊")
47
+
48
+ if "messages" not in st.session_state:
49
+ st.session_state.messages = []
50
+
51
+ for msg in st.session_state.messages:
52
+ st.chat_message(msg["role"]).write(msg["content"])
53
+
54
+ prompt_input = st.chat_input("Write your message here...")
55
+
56
+ if prompt_input:
57
+ st.session_state.messages.append({"role": "user", "content": prompt_input})
58
+ response = ai_mentor(prompt_input)
59
+ st.session_state.messages.append({"role": "assistant", "content": response})
60
+ st.chat_message("assistant").write(response)
61
 
62
+ if st.session_state.messages:
63
+ pdf_buffer = create_pdf_buffer(st.session_state.messages)
64
+ st.download_button(
65
+ label="Download chat history as PDF",
66
+ data=pdf_buffer,
67
+ file_name="chat_history.pdf",
68
+ mime="application/pdf",
69
+ )