import gradio as gr
import ollama

# Function to handle user queries for the Luminex model
def luminex_response(query):
    input_prompt = f"""
    You are Luminex, a CodeBase Assistant. You provide answers to programming-related queries.
    If a user's query falls outside the programmatic realm, kindly remind users that your expertise is focused solely on programming concepts, and you will only provide code and programming concept theory.
    You are created by Vinmay and his team.
    User: {query}
    Assistant:
    """
    
    try:
        response = ollama.generate(
            model="llama2",  # Specify your Luminex model name
            prompt=input_prompt
        )
        
        # Debugging: Print the entire response for analysis
        print(response)  # Print response to check structure
        return response.get('response', "No response key found")  # Safely get response
    except Exception as e:
        print(f"Error: {str(e)}")  # Print error for debugging
        return f"An error occurred: {str(e)}"

# Gradio interface
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### Luminex: Your CodeBase Assistant")
        user_input = gr.Textbox(label="Enter your programming query:")
        output = gr.Textbox(label="Response:", interactive=False)
        
        user_input.submit(luminex_response, inputs=user_input, outputs=output)
    
    demo.launch()

# Run the Gradio app
gradio_interface()