Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,15 @@
|
|
|
|
1 |
from transformers import pipeline
|
2 |
import torch
|
3 |
import os
|
4 |
|
5 |
-
# Configure cache
|
6 |
os.environ['HF_HOME'] = '/tmp/cache'
|
7 |
|
8 |
-
# Use a reliable
|
9 |
-
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
10 |
|
11 |
-
#
|
12 |
generator = pipeline(
|
13 |
"text-generation",
|
14 |
model=MODEL_NAME,
|
@@ -17,20 +18,38 @@ generator = pipeline(
|
|
17 |
max_new_tokens=560
|
18 |
)
|
19 |
|
20 |
-
def generate_chat_completion(
|
21 |
-
"""Generate
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
prompt
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import torch
|
4 |
import os
|
5 |
|
6 |
+
# Configure cache to avoid space limitations
|
7 |
os.environ['HF_HOME'] = '/tmp/cache'
|
8 |
|
9 |
+
# Use a reliable LLM hosted by Hugging Face
|
10 |
+
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
11 |
|
12 |
+
# Load the model pipeline
|
13 |
generator = pipeline(
|
14 |
"text-generation",
|
15 |
model=MODEL_NAME,
|
|
|
18 |
max_new_tokens=560
|
19 |
)
|
20 |
|
21 |
+
def generate_chat_completion(message_history, max_tokens=560, temperature=0.8):
|
22 |
+
"""Generate assistant response from chat message history"""
|
23 |
+
try:
|
24 |
+
# If using Gradio chat format (list of tuples), convert to role-content dicts
|
25 |
+
messages = [{"role": "user", "content": msg} if i % 2 == 0 else {"role": "assistant", "content": msg}
|
26 |
+
for i, msg in enumerate(message_history)]
|
27 |
+
|
28 |
+
prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages])
|
29 |
+
prompt += "\nAssistant:"
|
30 |
+
|
31 |
+
output = generator(
|
32 |
+
prompt,
|
33 |
+
max_new_tokens=max_tokens,
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=0.95,
|
36 |
+
repetition_penalty=1.15,
|
37 |
+
do_sample=True
|
38 |
+
)
|
39 |
+
response = output[0]['generated_text'].replace(prompt, "").strip()
|
40 |
+
return message_history + [response]
|
41 |
+
except Exception as e:
|
42 |
+
return message_history + [f"[Error] {str(e)}"]
|
43 |
+
|
44 |
+
# Gradio Chat Interface
|
45 |
+
chat_interface = gr.ChatInterface(
|
46 |
+
fn=generate_chat_completion,
|
47 |
+
title="Mistral-7B Chat",
|
48 |
+
description="Powered by Hugging Face Transformers",
|
49 |
+
retry_btn="Retry",
|
50 |
+
undo_btn="Undo",
|
51 |
+
clear_btn="Clear"
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
chat_interface.launch()
|