Spaces:
Sleeping
Sleeping
Upload gradio_app.py
Browse files- gradio_app.py +58 -0
gradio_app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# src/gradio_app.py
|
2 |
+
import gradio as gr
|
3 |
+
from agent import Agent
|
4 |
+
from create_database import load_and_process_dataset # Import from create_database.py
|
5 |
+
import os
|
6 |
+
import uuid
|
7 |
+
import urllib.request
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
+
|
13 |
+
def download_model():
|
14 |
+
model_url = "https://path/to/your/model.bin"
|
15 |
+
model_path = "model.bin"
|
16 |
+
|
17 |
+
if not os.path.exists(model_path):
|
18 |
+
print("Downloading model...")
|
19 |
+
urllib.request.urlretrieve(model_url, model_path)
|
20 |
+
print("Model downloaded successfully.")
|
21 |
+
|
22 |
+
def respond(
|
23 |
+
message,
|
24 |
+
history: list[tuple[str, str]],
|
25 |
+
system_message,
|
26 |
+
):
|
27 |
+
model_path = "model.bin" # Path to the downloaded model
|
28 |
+
db_path = "agent.db"
|
29 |
+
system_prompt = system_message
|
30 |
+
|
31 |
+
# Check if the model is downloaded
|
32 |
+
if not os.path.exists(model_path):
|
33 |
+
download_model()
|
34 |
+
|
35 |
+
# Check if the database exists, if not, initialize it
|
36 |
+
if not os.path.exists(db_path):
|
37 |
+
data_update_path = "data-update.txt"
|
38 |
+
keyword_dir = "keyword" # Updated keyword directory
|
39 |
+
load_and_process_dataset(data_update_path, keyword_dir, db_path)
|
40 |
+
|
41 |
+
agent = Agent(model_path, db_path, system_prompt)
|
42 |
+
user_id = str(uuid.uuid4()) # Generate a unique user ID for each session
|
43 |
+
|
44 |
+
response = agent.process_query(user_id, message)
|
45 |
+
return response
|
46 |
+
|
47 |
+
"""
|
48 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
49 |
+
"""
|
50 |
+
demo = gr.ChatInterface(
|
51 |
+
respond,
|
52 |
+
additional_inputs=[
|
53 |
+
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"),
|
54 |
+
],
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
demo.launch()
|