|
import os |
|
from dotenv import load_dotenv |
|
from langchain_community.vectorstores import Qdrant |
|
from langchain_huggingface import HuggingFaceEmbeddings |
|
from langchain.prompts import ChatPromptTemplate |
|
from langchain.schema.runnable import RunnablePassthrough |
|
from langchain.schema.output_parser import StrOutputParser |
|
from qdrant_client import QdrantClient, models |
|
from langchain_openai import ChatOpenAI |
|
import gradio as gr |
|
import logging |
|
from typing import List, Tuple |
|
from dataclasses import dataclass |
|
from datetime import datetime |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
from langchain_huggingface.llms import HuggingFacePipeline |
|
import spaces |
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as iface: |
|
gr.Image("Image.jpg", width=1200, height=300, show_label=False, show_download_button=False) |
|
gr.Markdown("# Mawared HR Assistant") |
|
gr.Markdown("Ask questions about the Mawared HR system, and this assistant will provide answers based on the available context and conversation history.") |
|
|
|
|
|
latest_response = gr.State("") |
|
|
|
with gr.Row(): |
|
chatbot = gr.Chatbot( |
|
height=400, |
|
show_label=False, |
|
type="messages" |
|
) |
|
|
|
with gr.Row(): |
|
|
|
copy_button = gr.Button("π Copy Last Response", visible=True) |
|
|
|
with gr.Row(): |
|
question_input = gr.Textbox( |
|
label="Ask a question:", |
|
placeholder="Type your question here...", |
|
scale=25 |
|
) |
|
clear_button = gr.Button("Clear Chat", scale=1) |
|
|
|
def copy_last_response(history): |
|
if history: |
|
|
|
for message in reversed(history): |
|
if message["role"] == "assistant": |
|
return message["content"] |
|
return "" |
|
|
|
|
|
def ask_question_gradio(question, history): |
|
try: |
|
|
|
chat_history.add_message("user", question) |
|
|
|
|
|
formatted_history = chat_history.get_formatted_history() |
|
|
|
|
|
rag_chain = create_rag_chain(formatted_history) |
|
|
|
|
|
response = "" |
|
for chunk in rag_chain.stream(question): |
|
response += chunk |
|
|
|
|
|
chat_history.add_message("assistant", response) |
|
|
|
|
|
history.append({"role": "user", "content": question}) |
|
history.append({"role": "assistant", "content": response}) |
|
|
|
return "", history |
|
except Exception as e: |
|
logger.error(f"Error during question processing: {e}") |
|
return "", history + [{"role": "assistant", "content": "An error occurred. Please try again later."}] |
|
|
|
def clear_chat(): |
|
chat_history.clear() |
|
return [], "" |
|
|
|
|
|
question_input.submit( |
|
ask_question_gradio, |
|
inputs=[question_input, chatbot], |
|
outputs=[question_input, chatbot] |
|
) |
|
|
|
clear_button.click( |
|
clear_chat, |
|
outputs=[chatbot, question_input] |
|
) |
|
|
|
|
|
copy_button.click( |
|
copy_last_response, |
|
inputs=[chatbot], |
|
outputs=[], |
|
_js=""" |
|
async (response) => { |
|
await navigator.clipboard.writeText(response); |
|
// Optional: Show a toast notification |
|
const toast = document.createElement('div'); |
|
toast.textContent = 'Response copied to clipboard!'; |
|
toast.style.position = 'fixed'; |
|
toast.style.bottom = '20px'; |
|
toast.style.right = '20px'; |
|
toast.style.backgroundColor = '#4CAF50'; |
|
toast.style.color = 'white'; |
|
toast.style.padding = '15px'; |
|
toast.style.borderRadius = '5px'; |
|
toast.style.zIndex = '1000'; |
|
document.body.appendChild(toast); |
|
setTimeout(() => toast.remove(), 2000); |
|
} |
|
""" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |