CoolFace
Apppublic

technojj1s/AImodel-Agent

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
streamlit_app.py136 linesDownload Raw Back to src
1import streamlit as st2import requests3import json4 5"""6# AI Chatbot with Llama 3.2 ๐Ÿค–7 8Powered by Llama 3.2 running locally via Ollama.9"""10 11# Ollama API endpoint12OLLAMA_API = "http://model-runner.docker.internal:12434/api/generate"13MODEL_NAME = "llama3.2"14 15def generate_response(prompt, chat_history):16    """Generate response from Llama 3.2"""17    try:18        # Prepare the full context with chat history19        context = ""20        for msg in chat_history[-5:]:  # Last 5 messages for context21            role = "User" if msg["role"] == "user" else "Assistant"22            context += f"{role}: {msg['content']}\n"23        context += f"User: {prompt}\nAssistant:"24        25        # Call Ollama API26        response = requests.post(27            OLLAMA_API,28            json={29                "model": MODEL_NAME,30                "prompt": context,31                "stream": True32            },33            stream=True34        )35        36        full_response = ""37        for line in response.iter_lines():38            if line:39                json_response = json.loads(line)40                if "response" in json_response:41                    full_response += json_response["response"]42                    yield json_response["response"]43                44                if json_response.get("done", False):45                    break46        47        return full_response48    49    except requests.exceptions.ConnectionError:50        yield "โš ๏ธ Error: Cannot connect to Ollama. Make sure Ollama is running on localhost:12434"51    except Exception as e:52        yield f"โš ๏ธ Error: {str(e)}"53 54# Initialize chat history55if "messages" not in st.session_state:56    st.session_state.messages = []57 58# Display chat messages from history59for message in st.session_state.messages:60    with st.chat_message(message["role"]):61        st.markdown(message["content"])62 63# Accept user input64if prompt := st.chat_input("What's on your mind?"):65    # Add user message to chat history66    st.session_state.messages.append({"role": "user", "content": prompt})67    68    # Display user message69    with st.chat_message("user"):70        st.markdown(prompt)71    72    # Generate bot response73    with st.chat_message("assistant"):74        message_placeholder = st.empty()75        full_response = ""76        77        # Stream response from Llama78        for chunk in generate_response(prompt, st.session_state.messages[:-1]):79            full_response += chunk80            message_placeholder.markdown(full_response + "โ–Œ")81        82        message_placeholder.markdown(full_response)83    84    # Add assistant response to chat history85    st.session_state.messages.append({"role": "assistant", "content": full_response})86 87# Sidebar with options88st.sidebar.title("Chat Options")89 90# Model info91st.sidebar.markdown(f"""92### Current Model93**{MODEL_NAME}**94Running on localhost95""")96 97if st.sidebar.button("Clear Chat History"):98    st.session_state.messages = []99    st.rerun()100 101st.sidebar.markdown("---")102 103# Check Ollama connection104try:105    check_response = requests.get("http://model-runner.docker.internal:12434/api/tags", timeout=2)106    if check_response.status_code == 200:107        st.sidebar.success("โœ… Ollama Connected")108        models = check_response.json().get("models", [])109        if models:110            st.sidebar.markdown("**Available Models:**")111            for model in models:112                st.sidebar.text(f"โ€ข {model.get('name', 'Unknown')}")113    else:114        st.sidebar.error("โŒ Ollama Not Responding")115except:116    st.sidebar.error("โŒ Ollama Not Connected")117    st.sidebar.markdown("""118    **To start Ollama:**119    ```bash120    ollama serve121    ollama pull llama3.2122    ```123    """)124 125st.sidebar.markdown("---")126st.sidebar.markdown("""127### About128This chatbot uses **Llama 3.2** running 129locally via Ollama for AI responses.130 131**Features:**132- Local AI processing133- Chat history with context134- Streaming responses135- No API costs136""")