parjun commited on
Commit
cf04477
Β·
verified Β·
1 Parent(s): 14b9ca5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -55
app.py CHANGED
@@ -1,56 +1,50 @@
1
- import gradio as gr
2
- from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
3
- from langchain_groq import ChatGroq
4
- from dotenv import load_dotenv
5
- import os
6
-
7
- load_dotenv()
8
-
9
- # Initialize Groq Chat model
10
- chat = ChatGroq(model="gemma2-9b-it", temperature=0.5)
11
-
12
- # Initialize session context
13
- conversation_history = [SystemMessage(content="You are a comedian AI assistant")]
14
-
15
- def chatbot_response(user_input):
16
- # Append user message
17
- conversation_history.append(HumanMessage(content=user_input))
18
-
19
- # Get model response
20
- response = chat.invoke(conversation_history)
21
-
22
- # Append bot message
23
- conversation_history.append(AIMessage(content=response.content))
24
-
25
- # Format chat history for display
26
- chat_log = ""
27
- for msg in conversation_history:
28
- if isinstance(msg, SystemMessage):
29
- chat_log += f"βš™οΈ *System Role*: {msg.content}\n\n"
30
- elif isinstance(msg, HumanMessage):
31
- chat_log += f"πŸ‘€ **User**: {msg.content}\n\n"
32
- elif isinstance(msg, AIMessage):
33
- chat_log += f"πŸ€– **Bot**: {msg.content}\n\n"
34
-
35
- return response.content, chat_log
36
-
37
- # Gradio interface layout
38
- with gr.Blocks() as demo:
39
- gr.Markdown("# πŸ€– Conversational Q&A Chatbot")
40
- gr.Markdown("Chat with a friendly AI powered by Groq’s `gemma2-9b-it` model.")
41
-
42
- with gr.Row():
43
- user_input = gr.Textbox(label="Enter your question")
44
- submit_btn = gr.Button("Ask")
45
-
46
- response_output = gr.Textbox(label="Response", interactive=False)
47
- chat_history = gr.Markdown()
48
-
49
- def handle_submit(input_text):
50
- response, history = chatbot_response(input_text)
51
- return response, history
52
-
53
- submit_btn.click(fn=handle_submit, inputs=user_input, outputs=[response_output, chat_history])
54
-
55
- # Launch app
56
  demo.launch()
 
1
+ import gradio as gr
2
+ from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
3
+ from langchain_groq import ChatGroq
4
+
5
+ # Session-wide context
6
+ conversation_history = [SystemMessage(content="You are a comedian AI assistant")]
7
+
8
+ def chatbot_response(api_key, user_input):
9
+ if not api_key:
10
+ return "Please enter your Groq API key.", ""
11
+
12
+ # Pass API key to ChatGroq
13
+ chat = ChatGroq(model="gemma2-9b-it", temperature=0.5, api_key=api_key)
14
+
15
+ # Append user input and get response
16
+ conversation_history.append(HumanMessage(content=user_input))
17
+ response = chat.invoke(conversation_history)
18
+ conversation_history.append(AIMessage(content=response.content))
19
+
20
+ # Format chat history
21
+ chat_log = ""
22
+ for msg in conversation_history:
23
+ if isinstance(msg, SystemMessage):
24
+ chat_log += f"βš™οΈ *System Role*: {msg.content}\n\n"
25
+ elif isinstance(msg, HumanMessage):
26
+ chat_log += f"πŸ‘€ **User**: {msg.content}\n\n"
27
+ elif isinstance(msg, AIMessage):
28
+ chat_log += f"πŸ€– **Bot**: {msg.content}\n\n"
29
+
30
+ return response.content, chat_log
31
+
32
+ # Gradio UI
33
+ with gr.Blocks() as demo:
34
+ gr.Markdown("# πŸ€– Conversational Q&A Chatbot")
35
+ gr.Markdown("Enter your Groq API key and chat below!")
36
+
37
+ api_key_box = gr.Textbox(label="πŸ”‘ Groq API Key", type="password")
38
+ user_input = gr.Textbox(label="πŸ’¬ Your Question")
39
+ submit_btn = gr.Button("Ask")
40
+
41
+ response_output = gr.Textbox(label="🧠 Response", interactive=False)
42
+ chat_history = gr.Markdown()
43
+
44
+ submit_btn.click(
45
+ fn=chatbot_response,
46
+ inputs=[api_key_box, user_input],
47
+ outputs=[response_output, chat_history]
48
+ )
49
+
 
 
 
 
 
 
50
  demo.launch()