Engr-Usman-Ali commited on
Commit
62c9e13
·
verified ·
1 Parent(s): 92fe91c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -22
app.py CHANGED
@@ -13,16 +13,19 @@ HF_API_KEY = st.secrets["HF_API_KEY"]
13
  # =======================
14
  st.sidebar.title("⚙️ Settings")
15
  provider_choice = st.sidebar.radio(
16
- "Choose Provider", ["Groq", "Hugging Face"]
 
17
  )
18
 
19
  if provider_choice == "Groq":
20
  model_choice = st.sidebar.selectbox(
21
- "Choose Groq Model", ["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
 
22
  )
23
  else:
24
  model_choice = st.sidebar.selectbox(
25
- "Choose HF Model", ["mistralai/Mixtral-8x7B-Instruct-v0.1", "tiiuae/falcon-7b-instruct"]
 
26
  )
27
 
28
  st.title("🤖 CodeCraft AI - Mini Copilot (Chat Edition)")
@@ -37,21 +40,21 @@ if "debug_chat" not in st.session_state:
37
  if "explain_chat" not in st.session_state:
38
  st.session_state.explain_chat = []
39
 
 
40
  # =======================
41
  # Helper functions
42
  # =======================
43
- def call_groq(chat_history: list[tuple[str, str]], system_prompt: str) -> str:
44
- try:
45
- response = client.chat.completions.create(
46
- model=model_choice,
47
- messages=[{"role": "system", "content": system_prompt}] + [{"role": role, "content": msg} for role, msg in chat_history],
48
- temperature=0.4
49
- )
50
- return response.choices[0].message.content
51
- except Exception as e:
52
- return f"⚠️ Groq Error: {str(e)}"
53
 
54
- def call_hf(prompt: str) -> str:
 
55
  headers = {"Authorization": f"Bearer {HF_API_KEY}"}
56
  payload = {"inputs": prompt}
57
  response = requests.post(
@@ -68,22 +71,24 @@ def call_hf(prompt: str) -> str:
68
  return str(result)
69
  return f"⚠️ HF Error: {response.text}"
70
 
71
- def get_ai_response(chat_history: list[tuple[str, str]], system_prompt: str) -> str:
 
72
  if provider_choice == "Groq":
73
  return call_groq(chat_history, system_prompt)
74
  else:
 
75
  prompt = system_prompt + "\n\n"
76
  for role, msg in chat_history:
77
  prompt += f"{role.upper()}: {msg}\n"
78
  return call_hf(prompt)
79
 
80
 
81
-
82
  # =======================
83
  # Chat UI
84
  # =======================
85
  def chat_ui(tab_name, chat_history, system_prompt, input_key):
86
  st.subheader(tab_name)
 
87
  # --- Chat history display ---
88
  with st.container():
89
  for role, msg in chat_history:
@@ -106,20 +111,27 @@ def chat_ui(tab_name, chat_history, system_prompt, input_key):
106
 
107
  # --- Input bar + send button in one row ---
108
  col1, col2 = st.columns([10, 1])
109
- with col1:
110
- user_input = st.text_input("Type your message...", key=input_key, label_visibility="collapsed")
111
- with col2:
112
- send_btn = st.button("➤", key=input_key + "_send")
 
 
 
 
 
113
  # --- Handle input ---
114
  if send_btn and user_input.strip():
115
  chat_history.append(("user", user_input.strip()))
 
116
  with st.spinner("Thinking..."):
117
  ai_msg = get_ai_response(chat_history, system_prompt)
118
- chat_history.append(("assistant", ai_msg))
 
 
119
  st.rerun()
120
 
121
 
122
-
123
  # =======================
124
  # Tabs
125
  # =======================
 
13
  # =======================
14
  st.sidebar.title("⚙️ Settings")
15
  provider_choice = st.sidebar.radio(
16
+ "Choose Provider",
17
+ ["Groq", "Hugging Face"]
18
  )
19
 
20
  if provider_choice == "Groq":
21
  model_choice = st.sidebar.selectbox(
22
+ "Choose Groq Model",
23
+ ["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
24
  )
25
  else:
26
  model_choice = st.sidebar.selectbox(
27
+ "Choose HF Model",
28
+ ["mistralai/Mixtral-8x7B-Instruct-v0.1", "tiiuae/falcon-7b-instruct"]
29
  )
30
 
31
  st.title("🤖 CodeCraft AI - Mini Copilot (Chat Edition)")
 
40
  if "explain_chat" not in st.session_state:
41
  st.session_state.explain_chat = []
42
 
43
+
44
  # =======================
45
  # Helper functions
46
  # =======================
47
+ def call_groq(chat_history, system_prompt):
48
+ response = client.chat.completions.create(
49
+ model=model_choice,
50
+ messages=[{"role": "system", "content": system_prompt}]
51
+ + [{"role": role, "content": msg} for role, msg in chat_history],
52
+ temperature=0.4
53
+ )
54
+ return response.choices[0].message.content
 
 
55
 
56
+
57
+ def call_hf(prompt):
58
  headers = {"Authorization": f"Bearer {HF_API_KEY}"}
59
  payload = {"inputs": prompt}
60
  response = requests.post(
 
71
  return str(result)
72
  return f"⚠️ HF Error: {response.text}"
73
 
74
+
75
+ def get_ai_response(chat_history, system_prompt):
76
  if provider_choice == "Groq":
77
  return call_groq(chat_history, system_prompt)
78
  else:
79
+ # Convert history into plain text prompt for Hugging Face
80
  prompt = system_prompt + "\n\n"
81
  for role, msg in chat_history:
82
  prompt += f"{role.upper()}: {msg}\n"
83
  return call_hf(prompt)
84
 
85
 
 
86
  # =======================
87
  # Chat UI
88
  # =======================
89
  def chat_ui(tab_name, chat_history, system_prompt, input_key):
90
  st.subheader(tab_name)
91
+
92
  # --- Chat history display ---
93
  with st.container():
94
  for role, msg in chat_history:
 
111
 
112
  # --- Input bar + send button in one row ---
113
  col1, col2 = st.columns([10, 1])
114
+ with col1:
115
+ user_input = st.text_input(
116
+ "Type your message...",
117
+ key=input_key,
118
+ label_visibility="collapsed"
119
+ )
120
+ with col2:
121
+ send_btn = st.button("➤", key=input_key + "_send")
122
+
123
  # --- Handle input ---
124
  if send_btn and user_input.strip():
125
  chat_history.append(("user", user_input.strip()))
126
+
127
  with st.spinner("Thinking..."):
128
  ai_msg = get_ai_response(chat_history, system_prompt)
129
+
130
+ chat_history.append(("assistant", ai_msg))
131
+ st.session_state[input_key] = "" # clear input
132
  st.rerun()
133
 
134
 
 
135
  # =======================
136
  # Tabs
137
  # =======================