0o7Hunk commited on
Commit
ecbf278
Β·
verified Β·
1 Parent(s): 9659b3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -12
app.py CHANGED
@@ -1,41 +1,76 @@
1
  import streamlit as st
2
- from groq import Groq
3
- import os
4
 
5
- # API key from secrets
6
- client = Groq(api_key=os.getenv("GROQ_API_KEY"))
7
 
8
- st.set_page_config(page_title="AI Chatbot", page_icon="πŸ€–")
 
 
9
 
 
10
  st.title("πŸ€– AI Chatbot (Groq)")
11
- st.write("Powered by Groq ⚑")
12
 
13
- # Chat memory
14
  if "messages" not in st.session_state:
15
  st.session_state.messages = []
16
 
17
- # Show messages
18
  for msg in st.session_state.messages:
19
  with st.chat_message(msg["role"]):
20
  st.markdown(msg["content"])
21
 
22
- # Input
23
  user_input = st.chat_input("Type your message...")
24
 
25
  if user_input:
 
26
  st.session_state.messages.append({"role": "user", "content": user_input})
27
-
28
  with st.chat_message("user"):
29
  st.markdown(user_input)
30
 
 
31
  with st.chat_message("assistant"):
32
  with st.spinner("Thinking..."):
33
  response = client.chat.completions.create(
34
- model="openai/gpt-oss-120b",
35
  messages=st.session_state.messages,
36
  max_tokens=500
37
  )
38
  reply = response.choices[0].message.content
39
  st.markdown(reply)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- st.session_state.messages.append({"role": "assistant", "content": reply})
 
 
 
 
 
1
  import streamlit as st
2
+ from pyngrok import ngrok
 
3
 
4
+ # Kill existing tunnels to avoid conflicts
5
+ ngrok.kill()
6
 
7
+ # Connect to port 8501 (default for Streamlit)
8
+ public_url = ngrok.connect(8501)
9
+ st.write(f"Ngrok tunnel URL: {public_url}")
10
 
11
+ # Your main Streamlit app code
12
  st.title("πŸ€– AI Chatbot (Groq)")
13
+ st.write("Fast AI chatbot πŸš€")
14
 
15
+ # Initialize chat messages in session state
16
  if "messages" not in st.session_state:
17
  st.session_state.messages = []
18
 
19
+ # Display chat messages
20
  for msg in st.session_state.messages:
21
  with st.chat_message(msg["role"]):
22
  st.markdown(msg["content"])
23
 
24
+ # User input
25
  user_input = st.chat_input("Type your message...")
26
 
27
  if user_input:
28
+ # Append user message
29
  st.session_state.messages.append({"role": "user", "content": user_input})
 
30
  with st.chat_message("user"):
31
  st.markdown(user_input)
32
 
33
+ # Generate response
34
  with st.chat_message("assistant"):
35
  with st.spinner("Thinking..."):
36
  response = client.chat.completions.create(
37
+ model="mixtral-8x7b-32768", # or your preferred model
38
  messages=st.session_state.messages,
39
  max_tokens=500
40
  )
41
  reply = response.choices[0].message.content
42
  st.markdown(reply)
43
+ # Append assistant response
44
+ st.session_state.messages.append({"role": "assistant", "content": reply})
45
+
46
+ # Connect ngrok to port 8501 (Streamlit default port)
47
+ from pyngrok import ngrok
48
+
49
+ # Disconnect all existing tunnels
50
+ public_url = ngrok.connect(8501)
51
+ st.write(f"πŸ‘‰ Your app is live at: {public_url}")
52
+ """
53
+
54
+ # Save the code to a Python file
55
+ with open("streamlit_app.py", "w") as f:
56
+ f.write(app_code)
57
+
58
+ # Step 4: Run the Streamlit app
59
+ !streamlit run streamlit_app.py &>/dev/null &
60
+
61
+ import json
62
+ import time
63
+ from pyngrok import ngrok
64
+
65
+
66
+ # Connect to port 8501
67
+ public_url = ngrok.connect(8501)
68
+
69
+ # Wait a moment to ensure the tunnel is established
70
+ time.sleep(2)
71
 
72
+ # Get the public URL
73
+ tunnels = ngrok.get_tunnels()
74
+ for tunnel in tunnels:
75
+ if tunnel.public_url:
76
+ print(f"Your Streamlit app is live at: {tunnel.public_url}")