Spaces:
Sleeping
Sleeping
import os | |
from langchain.prompts import PromptTemplate | |
from langchain.llms import OpenAI | |
import gradio as gr | |
import random | |
import time | |
from gradio_client import Client | |
os.environ["OPENAI_API_KEY"] = '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' | |
def get_response(query, history, llm): | |
client = Client("https://traversaal-fitx-ai.hf.space/") | |
result = client.predict( | |
query, # str in 'query' Textbox component | |
history, # str in 'history' Textbox component | |
api_name="/predict" | |
) | |
return result | |
llm = prepare_resources() | |
custom_css = """ | |
<style> | |
.gr-chatbox-container { | |
border: 2px solid #007BFF; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
.gr-chatbox-container .chat-history .history-item:nth-child(even) { | |
background-color: #f0f0f0; | |
border-radius: 5px; | |
margin: 5px 0; | |
padding: 10px; | |
} | |
.gr-chatbox-container .chat-input .text-box { | |
border: 2px solid #007BFF; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
.gr-chatbox-container .chat-input button { | |
background-color: #007BFF; | |
color: white; | |
border: none; | |
border-radius: 5px; | |
padding: 10px 20px; | |
cursor: pointer; | |
transition: background-color 0.3s; | |
} | |
.gr-chatbox-container .chat-input button:hover { | |
background-color: #0056b3; | |
} | |
.gr-chatbox-container .chat-title { | |
font-size: 24px; | |
font-weight: bold; | |
margin-bottom: 10px; | |
} | |
</style> | |
""" | |
def respond(message, chat_history): | |
if not chat_history: | |
# Display a welcome message if chat_history is empty | |
welcome_message = "Welcome to the AI Trainer Chatbot! I'm here to assist you with your questions." | |
chat_history = [(welcome_message, get_response(welcome_message, chat_history, llm))] | |
bot_message = get_response(message, chat_history, llm) | |
chat_history.append(bot_message) | |
time.sleep(0.5) | |
return "", chat_history | |
# Display the welcome message before creating the Gradio interface | |
welcome_message = "Welcome to the AI Trainer Chatbot! I'm here to assist you with your questions." | |
print(welcome_message) | |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: | |
chatbot_title = gr.HTML('<div class="chat-title">AI Trainer Chatbot</div>') | |
chatbot = gr.Chatbot() | |
msg = gr.Textbox(placeholder="I am your personal AI Trainer. Ask me a question") | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
demo.launch(debug=True) | |