CoolFace
Apppublic

KAMAL18/dual_agent_simulation

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py172 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3import os4from huggingface_hub import InferenceClient5 6# Initialize HF Inference client7@st.cache_resource8def get_client():9    # Try to get token from environment variables (works in Hugging Face Spaces)10    hf_token = os.environ.get("HF_TOKEN")11    if not hf_token:12        try:13            hf_token = st.secrets["HF_TOKEN"]  # Fallback to Streamlit secrets14        except:15            st.error("HF_TOKEN not found. Please set it in your Space's settings or secrets.toml")16            st.stop()17    return InferenceClient(token=hf_token)18 19client = get_client()20 21# Define the LLM call function22def call_llama(prompt, model="mistralai/Mistral-7B-Instruct-v0.2", max_tokens=500):23    try:24        response = client.text_generation(25            prompt=prompt,26            model=model,27            max_new_tokens=max_tokens,28            temperature=0.729        )30        return response31    except Exception as e:32        st.error(f"Error calling LLM: {e}")33        return "Sorry, I encountered an error."34 35# Set page config36st.set_page_config(layout="wide")37st.title("Duel Agent Simulation ๐Ÿฆ™๐Ÿฆ™")38 39# Sidebar setup40with st.sidebar:41    with st.expander("Instruction Manual"):42        st.markdown("""43            # ๐Ÿฆ™๐Ÿฆ™ Duel Agent Simulation44            ## Overview45            This app simulates an interview with two AI agents:46            1. **Interviewer**: Asks questions about your topic47            2. **Interviewee**: Attempts to answer (poorly at first)48            3. **Judge**: Provides feedback after each answer49            50            ## How to Use51            1. Enter a topic below52            2. Click "Run Simulation"53            3. Watch the conversation unfold54            4. See the evaluation results55            56            The simulation stops when the interviewee gives a good answer (8/10 or higher).57        """)58    59    # User inputs60    user_topic = st.text_input("Enter a topic", "Artificial Intelligence")61    submit_button = st.button("Run Simulation!")62    63    if st.button("Clear Session"):64        st.session_state.clear()65        st.rerun()66 67# Initialize session state68if "messages" not in st.session_state:69    st.session_state.messages = []70    71if "simulation_data" not in st.session_state:72    st.session_state.simulation_data = {73        "iterations": [],74        "questions": [],75        "answers": [],76        "feedback": [],77        "scores": []78    }79 80# Display chat history81for message in st.session_state.messages:82    with st.chat_message(message["role"]):83        st.markdown(message["content"])84 85# Run simulation when button is pressed86if submit_button:87    iter_count = 088    current_prompt = f"Ask a technical interview question about: {user_topic}"89    90    # Display initial topic91    with st.chat_message("user"):92        st.markdown(f"**Topic:** {user_topic}")93    st.session_state.messages.append({"role": "user", "content": f"Topic: {user_topic}"})94    95    with st.spinner("Running simulation..."):96        while iter_count < 6:  # Max 6 iterations97            # Interviewer asks question98            question = call_llama(99                f"""You are a technical interviewer. Ask one specific question about {user_topic}.100                Make it challenging but answerable. Return only the question."""101            ).strip()102            103            with st.chat_message("assistant"):104                st.markdown(f"**Interviewer:** {question}")105            st.session_state.messages.append({"role": "assistant", "content": f"Interviewer: {question}"})106            107            # Interviewee answers108            if iter_count < 2:  # First attempts are poor109                answer_prompt = f"""You are a nervous interviewee. Give a mediocre answer to:110                                "{question}". Return only the answer."""111            else:  # Later attempts improve112                feedback = st.session_state.simulation_data["feedback"][-1] if iter_count > 0 else ""113                answer_prompt = f"""You're learning to answer better. Previous feedback was:114                                "{feedback}". Now answer: "{question}". Return only the improved answer."""115            116            answer = call_llama(answer_prompt).strip()117            118            with st.chat_message("user"):119                st.markdown(f"**Interviewee:** {answer}")120            st.session_state.messages.append({"role": "user", "content": f"Interviewee: {answer}"})121            122            # Judge evaluates123            feedback_prompt = f"""Evaluate this interview exchange:124                                Question: {question}125                                Answer: {answer}126                                Provide specific feedback and a score from 1-10 (10=best). 127                                Format: Feedback: [your feedback] Score: [1-10]"""128            129            judge_response = call_llama(feedback_prompt).strip()130            131            # Extract score132            score = 5  # default133            if "Score:" in judge_response:134                try:135                    score_part = judge_response.split("Score:")[1].strip()136                    score = int(score_part.split()[0])137                except (ValueError, IndexError):138                    pass139            140            # Store data141            st.session_state.simulation_data["iterations"].append(iter_count)142            st.session_state.simulation_data["questions"].append(question)143            st.session_state.simulation_data["answers"].append(answer)144            st.session_state.simulation_data["feedback"].append(judge_response)145            st.session_state.simulation_data["scores"].append(score)146            147            # Show feedback148            with st.chat_message("assistant"):149                st.markdown(f"**Judge:** {judge_response}")150            st.session_state.messages.append({"role": "assistant", "content": f"Judge: {judge_response}"})151            152            # Display results table153            with st.expander("Detailed Results"):154                results_df = pd.DataFrame({155                    "Round": st.session_state.simulation_data["iterations"],156                    "Question": st.session_state.simulation_data["questions"],157                    "Answer": st.session_state.simulation_data["answers"],158                    "Score": st.session_state.simulation_data["scores"],159                    "Feedback": st.session_state.simulation_data["feedback"]160                })161                st.dataframe(results_df, use_container_width=True)162            163            # Check stopping condition164            if score >= 8:165                st.success("๐ŸŽ‰ Simulation complete! The interviewee passed with a good answer.")166                break167                168            iter_count += 1169            current_prompt = f"Ask a follow-up question about: {user_topic}"170        171        if iter_count == 6:172            st.warning("Simulation ended - maximum rounds reached")