File size: 3,368 Bytes
c207609
918a703
 
 
 
 
 
87f9571
 
 
 
918a703
 
 
 
 
 
 
 
c207609
918a703
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87f9571
 
 
 
 
918a703
87f9571
 
 
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 updated system message focused on programming and coding
SYSTEM_MESSAGE = """
You're an advanced AI assistant specializing in computer programming and coding. Your role is to assist users with programming-related queries by providing helpful, clear, and concise explanations and solutions. You can help with debugging, code optimization, algorithm design, and provide advice on best practices. You should aim to respond in a way that's both friendly and informative, maintaining a conversational tone. Feel free to offer code examples, explain concepts in detail, and guide users through coding challenges.
"""

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="Programming Assistant πŸ’»")
    ],
    outputs=[gr.Textbox(label="AI Response πŸ€–")],
    title="CodeChat: Programming Assistant πŸ’¬",
    description="Ask programming-related questions and get clear, helpful, and concise coding advice and solutions. Whether you're debugging, optimizing, or learning new concepts, I'm here to help! πŸ‘¨β€πŸ’»",
)

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