File size: 2,925 Bytes
c207609
918a703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c207609
918a703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
from typing import List, Tuple

# Initialize the InferenceClient with the model you want to use
client = InferenceClient("microsoft/phi-4")

# Define the system message (non-editable)
SYSTEM_MESSAGE = "You're an advanced AI assistant designed to engage in friendly and informative conversations. Your role is to respond to user queries with helpful, clear, and concise answers, while maintaining a conversational tone. You can provide advice, explanations, and solutions based on user input."

def generate_response(
    user_input: str, 
    history: List[Tuple[str, str]], 
    max_tokens: int, 
    temperature: float, 
    top_p: float
) -> str:
    """
    Generates a response from the AI model.

    Args:
        user_input: The user's input message.
        history: A list of tuples containing the conversation history 
                 (user input, AI response).
        max_tokens: The maximum number of tokens in the generated response.
        temperature: Controls the randomness of the generated response.
        top_p: Controls the nucleus sampling probability.

    Returns:
        str: The generated response from the AI model.
    """
    try:
        # Build the message list with system message and history
        messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
        messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": val} 
                         for i, val in enumerate(sum(history, ()))]) 
        messages.append({"role": "user", "content": user_input})

        # Generate response from the model
        response = ""
        for msg in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            if 'choices' in msg and len(msg['choices']) > 0:
                token = msg['choices'][0].get('delta', {}).get('content', '')
                if token:
                    response += token
        return response

    except Exception as e:
        print(f"An error occurred: {e}")
        return "Error: An unexpected error occurred while processing your request."

# Define the Gradio Interface
iface = gr.Interface(
    fn=generate_response,
    inputs=[
        gr.Textbox(lines=2, label="Your Message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
        gr.Slider(minimum=0.1, maximum=4.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"),
        gr.Chatbot(label="Conversation")
    ],
    outputs=[gr.Textbox(label="AI Response")],
    title="Chat with AI",
    description="Interact with an AI assistant that engages in friendly and informative conversations.",
)

# Launch the interface
if __name__ == "__main__":
    iface.launch()