parjun commited on
Commit
a1fa0ea
·
verified ·
1 Parent(s): c60614a

Upload app_grad.py

Browse files
Files changed (1) hide show
  1. app_grad.py +56 -0
app_grad.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()