Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import re
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize the model client
|
| 10 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
|
@@ -39,7 +49,7 @@ def respond(
|
|
| 39 |
try:
|
| 40 |
for token in client.chat_completion(
|
| 41 |
messages,
|
| 42 |
-
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p,
|
| 45 |
stream=True,
|
|
@@ -54,7 +64,11 @@ def respond(
|
|
| 54 |
yield response
|
| 55 |
|
| 56 |
# Save the conversation after completion
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
yield f"I apologize, but I encountered an error: {str(e)}"
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import re
|
| 4 |
+
import json
|
| 5 |
+
from typing import List, Tuple
|
| 6 |
|
| 7 |
+
# Simple chat history functions
|
| 8 |
+
def load_history(filename="conversation_history.json"):
|
| 9 |
+
try:
|
| 10 |
+
with open(filename, "r") as f:
|
| 11 |
+
return json.load(f)
|
| 12 |
+
except FileNotFoundError:
|
| 13 |
+
return []
|
| 14 |
+
|
| 15 |
+
def save_history(history, filename="conversation_history.json"):
|
| 16 |
+
with open(filename, "w") as f:
|
| 17 |
+
json.dump(history, f)
|
| 18 |
|
| 19 |
# Initialize the model client
|
| 20 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
|
|
|
| 49 |
try:
|
| 50 |
for token in client.chat_completion(
|
| 51 |
messages,
|
| 52 |
+
max_tokens=max_tokens,
|
| 53 |
temperature=temperature,
|
| 54 |
top_p=top_p,
|
| 55 |
stream=True,
|
|
|
|
| 64 |
yield response
|
| 65 |
|
| 66 |
# Save the conversation after completion
|
| 67 |
+
chat_history = load_history()
|
| 68 |
+
chat_history.append((message, response))
|
| 69 |
+
if len(chat_history) > 100: # Keep last 100 messages
|
| 70 |
+
chat_history = chat_history[-100:]
|
| 71 |
+
save_history(chat_history)
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
yield f"I apologize, but I encountered an error: {str(e)}"
|