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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# OpenAI API setup
|
| 7 |
+
openai.api_key = os.getenv("GROQ_API_KEY")
|
| 8 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
| 9 |
+
|
| 10 |
+
# File to store conversation history
|
| 11 |
+
CONVERSATION_FILE = "conversation_history.json"
|
| 12 |
+
|
| 13 |
+
# Function to load conversation history
|
| 14 |
+
def load_history():
|
| 15 |
+
if not os.path.exists(CONVERSATION_FILE):
|
| 16 |
+
# Create the file with an empty list as default content
|
| 17 |
+
with open(CONVERSATION_FILE, "w") as file:
|
| 18 |
+
json.dump([], file)
|
| 19 |
+
try:
|
| 20 |
+
with open(CONVERSATION_FILE, "r") as file:
|
| 21 |
+
return json.load(file)
|
| 22 |
+
except json.JSONDecodeError:
|
| 23 |
+
return []
|
| 24 |
+
|
| 25 |
+
# Function to save conversation history
|
| 26 |
+
def save_history(history):
|
| 27 |
+
try:
|
| 28 |
+
with open(CONVERSATION_FILE, "w") as file:
|
| 29 |
+
json.dump(history, file, indent=4)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error saving history: {e}")
|
| 32 |
+
|
| 33 |
+
# Function to clear conversation history
|
| 34 |
+
def clear_conversation_history():
|
| 35 |
+
try:
|
| 36 |
+
with open(CONVERSATION_FILE, "w") as file:
|
| 37 |
+
json.dump([], file)
|
| 38 |
+
return "Conversation history cleared successfully."
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error clearing history: {e}"
|
| 41 |
+
|
| 42 |
+
# Function to get response from the LLM
|
| 43 |
+
def get_groq_response(message, history=[]):
|
| 44 |
+
try:
|
| 45 |
+
messages = [{"role": "system", "content": "Precise answer"}] + history + [{"role": "user", "content": message}]
|
| 46 |
+
response = openai.ChatCompletion.create(
|
| 47 |
+
model="llama-3.1-70b-versatile",
|
| 48 |
+
messages=messages
|
| 49 |
+
)
|
| 50 |
+
return response.choices[0].message["content"]
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Chatbot function
|
| 55 |
+
def chatbot(user_input, history):
|
| 56 |
+
# Load conversation history
|
| 57 |
+
conversation_history = history or load_history()
|
| 58 |
+
|
| 59 |
+
# Format history for the LLM
|
| 60 |
+
formatted_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, (msg, _) in enumerate(conversation_history)] + \
|
| 61 |
+
[{"role": "assistant", "content": response} for _, response in conversation_history]
|
| 62 |
+
|
| 63 |
+
# Get bot response
|
| 64 |
+
bot_response = get_groq_response(user_input, formatted_history)
|
| 65 |
+
|
| 66 |
+
# Update history with the new conversation
|
| 67 |
+
conversation_history.append((user_input, bot_response))
|
| 68 |
+
|
| 69 |
+
# Save the updated history
|
| 70 |
+
save_history(conversation_history)
|
| 71 |
+
|
| 72 |
+
return conversation_history, conversation_history, "" # Clear the user input field
|
| 73 |
+
|
| 74 |
+
# Gradio Interface with enhanced UI/UX
|
| 75 |
+
with gr.Blocks(css="""
|
| 76 |
+
.gradio-container {
|
| 77 |
+
font-family: 'Arial', sans-serif;
|
| 78 |
+
background-color: #F2EFE7;
|
| 79 |
+
padding: 20px;
|
| 80 |
+
height: 100%;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
.gr-chatbot {
|
| 84 |
+
background-color: #FFFFFF;
|
| 85 |
+
border-radius: 10px;
|
| 86 |
+
padding: 20px;
|
| 87 |
+
max-height: 600px; /* Increased height */
|
| 88 |
+
overflow-y: auto;
|
| 89 |
+
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
|
| 90 |
+
scroll-behavior: smooth; /* Smooth scrolling */
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.user-message {
|
| 94 |
+
background-color: #9ACBD0;
|
| 95 |
+
color: #FFF;
|
| 96 |
+
padding: 12px;
|
| 97 |
+
border-radius: 8px;
|
| 98 |
+
margin: 10px 0;
|
| 99 |
+
max-width: 60%;
|
| 100 |
+
text-align: right;
|
| 101 |
+
float: right;
|
| 102 |
+
clear: both;
|
| 103 |
+
transition: transform 0.3s ease;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.bot-message {
|
| 107 |
+
background-color: #48A6A7;
|
| 108 |
+
color: #FFF;
|
| 109 |
+
padding: 12px;
|
| 110 |
+
border-radius: 8px;
|
| 111 |
+
margin: 10px 0;
|
| 112 |
+
max-width: 60%;
|
| 113 |
+
text-align: left;
|
| 114 |
+
float: left;
|
| 115 |
+
clear: both;
|
| 116 |
+
transition: transform 0.3s ease;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
.user-message:hover, .bot-message:hover {
|
| 120 |
+
transform: scale(1.05);
|
| 121 |
+
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
.gr-button {
|
| 125 |
+
background-color: #2973B2;
|
| 126 |
+
color: white;
|
| 127 |
+
padding: 10px 15px;
|
| 128 |
+
border-radius: 8px;
|
| 129 |
+
border: none;
|
| 130 |
+
transition: background-color 0.3s ease;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
.gr-button:hover {
|
| 134 |
+
background-color: #21689D;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
.gr-textbox input {
|
| 138 |
+
padding: 15px;
|
| 139 |
+
font-size: 16px;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.gr-markdown h1 {
|
| 143 |
+
color: #3A5A6E;
|
| 144 |
+
font-size: 28px;
|
| 145 |
+
text-align: center;
|
| 146 |
+
}
|
| 147 |
+
""") as demo:
|
| 148 |
+
gr.Markdown("# Mom: We have ChatGPT at Home, \n ChatGPT at Home:")
|
| 149 |
+
|
| 150 |
+
# Chatbot UI
|
| 151 |
+
chatbot_ui = gr.Chatbot()
|
| 152 |
+
user_input = gr.Textbox(label="Type your message here:", placeholder="Ask me anything...", lines=1)
|
| 153 |
+
clear_button = gr.Button("Clear History")
|
| 154 |
+
system_message = gr.Textbox(label="System Message", interactive=False)
|
| 155 |
+
|
| 156 |
+
history_state = gr.State(load_history())
|
| 157 |
+
|
| 158 |
+
# Chat interaction
|
| 159 |
+
user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
|
| 160 |
+
|
| 161 |
+
# Clear history button action
|
| 162 |
+
clear_button.click(clear_conversation_history, inputs=None, outputs=system_message)
|
| 163 |
+
clear_button.click(lambda: [], outputs=chatbot_ui) # Clear the chatbot UI
|
| 164 |
+
clear_button.click(lambda: [], outputs=history_state) # Reset the history state
|
| 165 |
+
|
| 166 |
+
# Launch the app
|
| 167 |
+
demo.launch()
|