Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- README.md +8 -5
- app.py +84 -0
- botm.png +0 -0
- readme.txt +2 -0
- user.png +0 -0
README.md
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
---
|
2 |
-
title: Mixtral
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
|
|
|
|
|
|
9 |
pinned: false
|
10 |
---
|
11 |
|
|
|
1 |
---
|
2 |
+
title: Mixtral Chat
|
3 |
+
emoji: π
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.37.2
|
8 |
app_file: app.py
|
9 |
+
models:
|
10 |
+
- mistralai/Mixtral-8x7B-v0.1
|
11 |
+
- mistralai/Mixtral-8x7B-Instruct-v0.1
|
12 |
pinned: false
|
13 |
---
|
14 |
|
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
client = InferenceClient(model="mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
+
|
6 |
+
def format_prompt(message, history):
|
7 |
+
prompt = "<s>"
|
8 |
+
for user_prompt, bot_response in history:
|
9 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
10 |
+
prompt += f" {bot_response}</s> "
|
11 |
+
prompt += f"[INST] {message} [/INST]"
|
12 |
+
return prompt
|
13 |
+
|
14 |
+
def should_stop_generation(output, stop_patterns):
|
15 |
+
for pattern in stop_patterns:
|
16 |
+
if pattern in output:
|
17 |
+
return True
|
18 |
+
return False
|
19 |
+
|
20 |
+
async def generate(
|
21 |
+
prompt, history, temperature=0.7, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
|
22 |
+
stop_patterns=None, max_loops=5
|
23 |
+
):
|
24 |
+
if stop_patterns is None:
|
25 |
+
stop_patterns = ["\n\n", ".", "The end", "Thank you"]
|
26 |
+
|
27 |
+
temperature = float(temperature)
|
28 |
+
if temperature < 1e-2:
|
29 |
+
temperature = 1e-2
|
30 |
+
top_p = float(top_p)
|
31 |
+
|
32 |
+
generate_kwargs = dict(
|
33 |
+
temperature=temperature,
|
34 |
+
max_new_tokens=max_new_tokens,
|
35 |
+
top_p=top_p,
|
36 |
+
repetition_penalty=repetition_penalty,
|
37 |
+
do_sample=True,
|
38 |
+
seed=42,
|
39 |
+
)
|
40 |
+
|
41 |
+
formatted_prompt = format_prompt(prompt, history)
|
42 |
+
|
43 |
+
output = ""
|
44 |
+
loop_count = 0
|
45 |
+
while loop_count < max_loops:
|
46 |
+
try:
|
47 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
48 |
+
async for response in stream:
|
49 |
+
output += response.token.text
|
50 |
+
yield output
|
51 |
+
if should_stop_generation(output, stop_patterns):
|
52 |
+
return # Stop if end pattern is detected
|
53 |
+
|
54 |
+
loop_count += 1 # Increment loop count to avoid infinite loops
|
55 |
+
|
56 |
+
# If the text isn't complete, use the last segment as a new prompt
|
57 |
+
formatted_prompt = format_prompt(output.split("\n")[-1], history)
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error during streaming: {e}")
|
61 |
+
break
|
62 |
+
|
63 |
+
# Non-streaming fallback
|
64 |
+
try:
|
65 |
+
response = client.text_generation(formatted_prompt, **generate_kwargs, stream=False, return_full_text=False)
|
66 |
+
output = response # Use response as a string if streaming failed
|
67 |
+
if not should_stop_generation(output, stop_patterns):
|
68 |
+
output += " [Additional text required to complete the response.]"
|
69 |
+
yield output
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Error during non-streaming generation: {e}")
|
72 |
+
|
73 |
+
mychatbot = gr.Chatbot(
|
74 |
+
avatar_images=["./user.png", "./botm.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,
|
75 |
+
)
|
76 |
+
|
77 |
+
demo = gr.ChatInterface(fn=generate,
|
78 |
+
chatbot=mychatbot,
|
79 |
+
title="Mixtral 8x7b AI Chatbot By wifix199",
|
80 |
+
retry_btn=None,
|
81 |
+
undo_btn=None
|
82 |
+
)
|
83 |
+
|
84 |
+
demo.queue().launch(server_name="10.172.2.37", server_port=5002, show_api=False, share=True)
|
botm.png
ADDED
readme.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Thanks to the Mistral AI Team for developing this awesome model.
|
2 |
+
Wifix199
|
user.png
ADDED