Spaces:
Running
Running
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() | |