File size: 1,520 Bytes
3ac9859
8d49c14
c782cb9
c65213a
 
 
3ac9859
c65213a
 
 
3ac9859
 
c65213a
 
3ac9859
c65213a
 
3ac9859
c65213a
 
 
 
 
8d49c14
c65213a
 
 
8d49c14
9424017
3ac9859
c65213a
8d49c14
 
c65213a
 
 
8d49c14
c65213a
 
 
3ac9859
c65213a
 
 
 
3ac9859
 
c65213a
 
 
3ac9859
 
b9ec1bd
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model directly
model_name = "microsoft/phi-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    trust_remote_code=True,
    device_map="auto"
)

# Simple chat history without streaming complexity
chat_history = []

def chat(message):
    global chat_history
    
    # Build the prompt from history
    prompt = ""
    for user_msg, bot_msg in chat_history:
        prompt += f"Human: {user_msg}\nAssistant: {bot_msg}\n"
    prompt += f"Human: {message}\nAssistant:"
    
    # Generate response (simple version)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(
        inputs.input_ids,
        max_new_tokens=256,
        temperature=0.7,
        do_sample=True
    )
    
    # Get response text
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    assistant_response = response[len(prompt):].strip()
    
    # Update history and return
    chat_history.append((message, assistant_response))
    return chat_history

# Create a simple interface
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox(placeholder="Type your message here...")
    clear = gr.Button("Clear")
    
    msg.submit(chat, msg, chatbot).then(lambda: "", None, msg)
    clear.click(lambda: [], None, chatbot)
    clear.click(lambda: [], None, msg)

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