K00B404 commited on
Commit
c61047e
·
verified ·
1 Parent(s): 1f9b776

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import gradio as gr
4
+ from datetime import datetime
5
+ from huggingface_hub import InferenceClient
6
+
7
+ # Constants
8
+ DEFAULT_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.1"
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
+ LOG_FILE = "chat_log.json"
11
+
12
+ def chat_with_model(system_prompt, user_message, max_tokens=500, model_id=""):
13
+ """
14
+ Generate a chat completion using the specified or default Mistral model.
15
+ Args:
16
+ system_prompt (str): The system prompt to set the context.
17
+ user_message (str): The user's input message.
18
+ max_tokens (int): Maximum number of tokens to generate.
19
+ model_id (str): The model ID to use. If empty, uses the default model.
20
+ Returns:
21
+ str: The model's response.
22
+ """
23
+ model_id = model_id.strip() if model_id else DEFAULT_MODEL_ID
24
+ client = InferenceClient(model_id, token=HF_TOKEN)
25
+ messages = [
26
+ {"role": "system", "content": system_prompt},
27
+ {"role": "user", "content": user_message}
28
+ ]
29
+ response = ""
30
+ try:
31
+ for message in client.chat_completion(
32
+ messages=messages,
33
+ max_tokens=max_tokens,
34
+ stream=True
35
+ ):
36
+ response += message.choices[0].delta.content or ""
37
+ except Exception as e:
38
+ response = f"Error: {str(e)}"
39
+
40
+ # Log the input and output
41
+ log_chat(system_prompt, user_message, response, max_tokens, model_id)
42
+
43
+ return response
44
+
45
+ def log_chat(system_prompt, user_message, response, max_tokens, model_id):
46
+ """
47
+ Log the chat details to a JSON file.
48
+ """
49
+ log_entry = {
50
+ "timestamp": datetime.now().isoformat(),
51
+ "system_prompt": system_prompt,
52
+ "user_message": user_message,
53
+ "response": response,
54
+ "max_tokens": max_tokens,
55
+ "model_id": model_id
56
+ }
57
+
58
+ try:
59
+ # Read existing log file if it exists
60
+ if os.path.exists(LOG_FILE):
61
+ with open(LOG_FILE, 'r') as f:
62
+ log_data = json.load(f)
63
+ else:
64
+ log_data = []
65
+
66
+ # Append new entry
67
+ log_data.append(log_entry)
68
+
69
+ # Write updated log back to file
70
+ with open(LOG_FILE, 'w') as f:
71
+ json.dump(log_data, f, indent=2)
72
+ except Exception as e:
73
+ print(f"Error logging chat: {str(e)}")
74
+
75
+ def create_gradio_interface():
76
+ """Create and configure the Gradio interface."""
77
+ return gr.Interface(
78
+ fn=chat_with_model,
79
+ inputs=[
80
+ gr.Textbox(label="System Prompt", placeholder="Enter the system prompt here..."),
81
+ gr.Textbox(label="User Message", placeholder="Ask a question..."),
82
+ gr.Slider(minimum=50, maximum=1000, value=500, step=50, label="Max Tokens"),
83
+ gr.Textbox(label="Model ID", placeholder=f"Enter model ID (default: {DEFAULT_MODEL_ID})")
84
+ ],
85
+ outputs=gr.Textbox(label="Response"),
86
+ title="Mistral Chatbot",
87
+ description="Chat with Mistral model using your own system prompts and choose your model.",
88
+ examples=[
89
+ ["You are a helpful AI assistant.", "What is the capital of France?", 500, ""],
90
+ ["You are an expert in Python programming.", "Explain list comprehensions.", 500, ""]
91
+ ]
92
+ )
93
+
94
+ if __name__ == "__main__":
95
+ iface = create_gradio_interface()
96
+ iface.launch(show_api=True, share=False, show_error=True)