Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from langchain_groq import ChatGroq | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain_core.output_parsers import JsonOutputParser | |
| import json | |
| import os | |
| os.environ["GROQ_API_KEY"] = os.getenv('GROQ_API') | |
| # Initialize Groq LLM | |
| llm = ChatGroq( | |
| model_name="llama-3.3-70b-specdec", | |
| temperature=0.7 | |
| ) | |
| # Define the expected JSON structure | |
| parser = JsonOutputParser(pydantic_object={ | |
| "type": "object", | |
| "properties": { | |
| "questions": { | |
| "type": "array", | |
| "items": { | |
| "type": "object", | |
| "properties": { | |
| "question": {"type": "string"}, | |
| "options": { | |
| "type": "array", | |
| "items": {"type": "string"} | |
| } | |
| }, | |
| "required": ["question", "options"] | |
| } | |
| }, | |
| "answers": { | |
| "type": "array", | |
| "items": {"type": "string"} | |
| } | |
| } | |
| }) | |
| # Create a simple prompt with correct variable usage | |
| prompt = ChatPromptTemplate.from_messages([ | |
| ("system", os.getenv('GROQ_TEMPLATE')), | |
| ("user", "{input}") | |
| ]) | |
| # Create the chain that guarantees JSON output | |
| chain = prompt | llm | parser | |
| def parse_product(description: str) -> dict: | |
| result = chain.invoke({"input": description}) | |
| return json.dumps(result, indent=2) | |
| # Define a function that takes three text inputs and three dropdown values, and returns a combined string. | |
| def combine_inputs(text1, text2, text3, dropdown1, dropdown2, dropdown3): | |
| description = f"""Skills: | |
| 1. {text1} ({dropdown1} level questions, 1 coding questions and 1 normal) | |
| 2. {text2} ({dropdown2} level questions, 2 questions) | |
| 3. {text3} ({dropdown3} level questions, 1 questions) | |
| """ | |
| return parse_product(description) | |
| # Create a Gradio interface with three text inputs and three dropdowns. | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| text1 = gr.Textbox(label="Skill 1") | |
| dropdown1 = gr.Dropdown(choices=["Very High", "Intermediate", "beginner"], label="Level") | |
| with gr.Row(): | |
| text2 = gr.Textbox(label="Skill 2") | |
| dropdown2 = gr.Dropdown(choices=["Very High", "Intermediate", "beginner"], label="Level") | |
| with gr.Row(): | |
| text3 = gr.Textbox(label="Skill 3") | |
| dropdown3 = gr.Dropdown(choices=["Very High", "Intermediate", "beginner"], label="Level") | |
| # Define the output textbox before using it in the button click | |
| output = gr.JSON(label="Output") | |
| greet_btn = gr.Button("Greet") | |
| greet_btn.click(combine_inputs, | |
| [text1, text2, text3, dropdown1, dropdown2, dropdown3], | |
| output) | |
| if __name__ == "__main__": | |
| demo.launch(show_error=True) | |