File size: 3,641 Bytes
6417feb
 
d419a22
 
 
6417feb
d419a22
 
 
6417feb
 
 
7df87c2
d419a22
 
 
6417feb
 
d419a22
 
f282186
7701fe2
d419a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f282186
d419a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74f452d
d419a22
 
f282186
d419a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6417feb
d419a22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6417feb
 
d419a22
 
 
6417feb
 
 
d419a22
6417feb
 
 
 
 
 
 
 
d419a22
 
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
105
106
107
108
109
110
111
import gradio as gr
from huggingface_hub import InferenceClient
from openai import OpenAI
import anthropic
import os

hf_client = InferenceClient()
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

MODEL_OPTIONS = {
    "Mixtral-8x7B-Instruct-v0.1": "mistralai/Mixtral-8x7B-Instruct-v0.1",
    "Qwen2-72B-Instruct": "Qwen/Qwen2-72B-Instruct",
    "GPT-3.5-turbo": "gpt-3.5-turbo",
    "GPT-4": "gpt-4",
    "claude-3-5-sonnet": "claude-3-5-sonnet-20240620",
}

def generate_text(model_choice, messages):
    full_response = ""
    
    if "Mixtral" in model_choice or "Qwen" in model_choice:
        prompt = "\n".join([f"Human: {m[0]}\nAI: {m[1] if m[1] else ''}" for m in messages])
        prompt += f"\nHuman: {messages[-1][0]}\nAI:"
        
        stream = hf_client.text_generation(
            model=MODEL_OPTIONS[model_choice],
            prompt=prompt,
            max_new_tokens=1000,
            temperature=0.7,
            do_sample=True,
            repetition_penalty=1.1,
            stream=True
        )
        for response in stream:
            full_response += response  # This line is correct now
            yield full_response
    
    elif "GPT" in model_choice:
        openai_messages = [{"role": "system", "content": "You are a helpful assistant."}]
        for m in messages:
            openai_messages.append({"role": "user", "content": m[0]})
            if m[1]:
                openai_messages.append({"role": "assistant", "content": m[1]})
        
        stream = openai_client.chat.completions.create(
            model=MODEL_OPTIONS[model_choice],
            messages=openai_messages,
            max_tokens=1000,
            temperature=0.7,
            stream=True
        )
        for chunk in stream:
            if chunk.choices[0].delta.content:
                full_response += chunk.choices[0].delta.content
                yield full_response
    
    elif "claude" in model_choice:
        claude_messages = []
        for msg in messages:
            if msg[0]:  # User message
                claude_messages.append({"role": "user", "content": msg[0]})
            if msg[1]:  # AI response
                claude_messages.append({"role": "assistant", "content": msg[1]})
        
        if not claude_messages:
            claude_messages = [{"role": "user", "content": "Hello"}]
        
        with anthropic_client.messages.stream(
            model=MODEL_OPTIONS[model_choice],
            max_tokens=1024,
            messages=claude_messages
        ) as stream:
            for text in stream.text_stream:
                full_response += text
                yield full_response
    
    else:
        yield "Unsupported model"

def user(user_message, history):
    history = history or []
    return "", history + [[user_message, None]]

def bot(history, model_choice):
    if not history:
        return []
    bot_message = generate_text(model_choice, history)
    history[-1][1] = ""
    for chunk in bot_message:
        history[-1][1] = chunk
        yield history

with gr.Blocks() as iface:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("Clear")
    
    model_dropdown = gr.Dropdown(
        choices=list(MODEL_OPTIONS.keys()),
        value="Mixtral-8x7B-Instruct-v0.1",
        label="Select Model"
    )

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, [chatbot, model_dropdown], chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

if __name__ == "__main__":
    iface.launch(debug=True)