Upload gitaAssis-grad.py
Browse files- gitaAssis-grad.py +58 -0
gitaAssis-grad.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Spiritual Gita AI assistant")]
|
7 |
+
|
8 |
+
def gita_chat(api_key, user_input):
|
9 |
+
if not api_key:
|
10 |
+
return "β οΈ Please enter your Groq API key.", ""
|
11 |
+
|
12 |
+
# Initialize model using the API key
|
13 |
+
chat = ChatGroq(model="gemma2-9b-it", temperature=0.5, api_key=api_key)
|
14 |
+
|
15 |
+
# Add user message
|
16 |
+
conversation_history.append(HumanMessage(content=user_input))
|
17 |
+
|
18 |
+
# Generate response
|
19 |
+
response = chat.invoke(conversation_history)
|
20 |
+
|
21 |
+
# Add bot message
|
22 |
+
conversation_history.append(AIMessage(content=response.content))
|
23 |
+
|
24 |
+
# Format full chat log
|
25 |
+
chat_log = ""
|
26 |
+
for msg in conversation_history:
|
27 |
+
if isinstance(msg, SystemMessage):
|
28 |
+
chat_log += f"βοΈ *System Role*: {msg.content}\n\n"
|
29 |
+
elif isinstance(msg, HumanMessage):
|
30 |
+
chat_log += f"π€ **User**: {msg.content}\n\n"
|
31 |
+
elif isinstance(msg, AIMessage):
|
32 |
+
chat_log += f"π€ **Bot**: {msg.content}\n\n"
|
33 |
+
|
34 |
+
return response.content, chat_log
|
35 |
+
|
36 |
+
# Build Gradio UI
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("# ποΈ Conversational Gita Assistant")
|
39 |
+
gr.Markdown(
|
40 |
+
"This chatbot is designed to offer spiritual guidance, inspired by the teachings of the **Bhagavad Gita**. πΏ\n\n"
|
41 |
+
"It has been initialized as a **Spiritual Gita AI assistant** β ready to answer questions and share verses with a compassionate and thoughtful tone. β¨\n\n"
|
42 |
+
"Enter your Groq API key to begin the conversation:"
|
43 |
+
)
|
44 |
+
|
45 |
+
api_key_input = gr.Textbox(label="π Groq API Key", type="password")
|
46 |
+
user_input = gr.Textbox(label="π§ Your Question or Prompt")
|
47 |
+
ask_button = gr.Button("Ask")
|
48 |
+
|
49 |
+
bot_response = gr.Textbox(label="π Response", interactive=False)
|
50 |
+
full_history = gr.Markdown()
|
51 |
+
|
52 |
+
ask_button.click(
|
53 |
+
fn=gita_chat,
|
54 |
+
inputs=[api_key_input, user_input],
|
55 |
+
outputs=[bot_response, full_history]
|
56 |
+
)
|
57 |
+
|
58 |
+
demo.launch()
|