MTC / src /gradio_app.py
userlocallm's picture
Upload 17 files
500516e verified
# src/gradio_app.py
import gradio as gr
from agent import Agent
from create_database import load_and_process_dataset # Import from create_database.py
import os
import uuid
import urllib.request
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def download_model():
model_url = "https://path/to/your/model.bin"
model_path = "model.bin"
if not os.path.exists(model_path):
print("Downloading model...")
urllib.request.urlretrieve(model_url, model_path)
print("Model downloaded successfully.")
def respond(
message,
history: list[tuple[str, str]],
system_message,
):
model_path = "model.bin" # Path to the downloaded model
db_path = "agent.db"
system_prompt = system_message
# Check if the model is downloaded
if not os.path.exists(model_path):
download_model()
# Check if the database exists, if not, initialize it
if not os.path.exists(db_path):
data_update_path = "data-update.txt"
keyword_dir = "keyword" # Updated keyword directory
load_and_process_dataset(data_update_path, keyword_dir, db_path)
agent = Agent(model_path, db_path, system_prompt)
user_id = str(uuid.uuid4()) # Generate a unique user ID for each session
response = agent.process_query(user_id, message)
return response
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="Vous êtes l'assistant intelligent de Les Chronique MTC. Votre rôle est d'aider les visiteurs en expliquant le contenu des Chroniques, Flash Infos et Chronique-FAQ de Michel Thomas. Utilisez le contexte fourni pour améliorer vos réponses et veillez à ce qu'elles soient précises et pertinentes.", label="System message"),
],
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)