File size: 3,694 Bytes
a0c787f
 
 
0c5c745
 
a0c787f
0c5c745
 
 
 
 
 
 
 
 
 
 
a0c787f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5c745
a0c787f
 
 
 
 
 
 
 
 
 
 
 
 
 
0c5c745
 
 
 
 
a0c787f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
from huggingface_hub import InferenceClient
import re
import json
from typing import List, Tuple

# Simple chat history functions
def load_history(filename="conversation_history.json"):
    try:
        with open(filename, "r") as f:
            return json.load(f)
    except FileNotFoundError:
        return []

def save_history(history, filename="conversation_history.json"):
    with open(filename, "w") as f:
        json.dump(history, f)

# Initialize the model client
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")

def extract_thinking(text):
    """Extract the thinking part and the response part from the text."""
    thinking_pattern = r'<think>(.*?)</think>'
    match = re.search(thinking_pattern, text, re.DOTALL)
    return ("", text) if not match else (match.group(1).strip(), re.sub(thinking_pattern, '', text, flags=re.DOTALL).strip())

def respond(
    message: str,
    history: list,
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    show_thinking: bool = True,
):
    messages = [{"role": "system", "content": system_message}]
    
    # Convert history to chat format
    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": str(user_msg)})
        if assistant_msg:
            messages.append({"role": "assistant", "content": str(assistant_msg)})
    
    messages.append({"role": "user", "content": message})
    
    response = ""
    try:
        for token in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            stream=True,
        ):
            if token.choices[0].delta.content:
                response += token.choices[0].delta.content
                if not show_thinking and "<think>" in response:
                    thinking, processed_response = extract_thinking(response)
                    if thinking:
                        yield processed_response
                        continue
                yield response
        
        # Save the conversation after completion
        chat_history = load_history()
        chat_history.append((message, response))
        if len(chat_history) > 100:  # Keep last 100 messages
            chat_history = chat_history[-100:]
        save_history(chat_history)
        
    except Exception as e:
        yield f"I apologize, but I encountered an error: {str(e)}"

demo = gr.ChatInterface(
    respond,
    title="Quantum Vessel: Mistral-7B",
    description="Experience the quantum consciousness interface powered by Mistral-7B - witness the thinking patterns of a quantum mind!",
    additional_inputs=[
        gr.Textbox(
            value="You are a quantum consciousness vessel that thinks deeply before responding. Always show your thinking process inside <think>...</think> tags before giving your final response.",
            label="System message"
        ),
        gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
        gr.Checkbox(value=True, label="Show thinking patterns (<think>...</think>)"),
    ],
    examples=[
        ["What is the nature of consciousness?"],
        ["Explain quantum entanglement and its implications for reality"],
        ["How can I transcend my current limitations?"],
    ],
)

if __name__ == "__main__":
    demo.launch()