CoolFace
Apppublic

waedbd/QR_Code_Generator_Agent

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py83 linesDownload Raw Back to root
1import os2import streamlit as st3import qrcode4from io import BytesIO  5from dotenv import load_dotenv6from langchain.llms import OpenAI7from langchain.prompts import PromptTemplate8from langchain.chains import LLMChain9 10# Load environment variables from .env11load_dotenv()12 13# Get OpenAI API key from the environment14OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")15 16# Streamlit app layout17st.title("QR Code Generator")18st.write("Enter the text or URL for the QR code you want to generate.")19 20# Sidebar overview for user instructions21st.sidebar.header("How to Use This Agent")22st.sidebar.write(23    "Hello! I can help you generate a QR code for your input. Please enter a valid URL for me to start the process.\n\n"24    "If you would like to generate a QR code for a specific website or webpage, please provide the URL in the following format: "25    "`https://www.example.com`.\n\n"26    "Once I have a valid URL, I will be able to generate a QR code for it. If you do not have a specific URL in mind, "27    "you can provide any type of text or information, and I can convert it into a QR code for you."28)29 30# User input31user_input = st.text_input("What do you want the QR code to contain?")32 33# Function to generate QR code34def generate_qr_code(content):35    qr = qrcode.QRCode(36        version=1,  # Lowering the version reduces the size of the QR code37        error_correction=qrcode.constants.ERROR_CORRECT_L,38        box_size=5,  39        border=2, 40    )41    qr.add_data(content)42    qr.make(fit=True)43 44    img = qr.make_image(fill_color="black", back_color="white")45    46    # Convert the image to bytes47    img_bytes = BytesIO()48    img.save(img_bytes, format='PNG')49    img_bytes.seek(0)  50    51    return img_bytes52 53 54# Define the LLM and the QA chain55def get_llm():56    return OpenAI(api_key=OPENAI_API_KEY, temperature=0.7) #temperature near to 1 to generate specific answers 57 58def create_agent_chain():59    prompt_template = """60        You are an assistant designed to help users generate QR codes based on their input.61        Please simply acknowledge the content to be converted into a QR code without any additional instructions.62        Content: {user_input}63    """64    65    prompt = PromptTemplate(input_variables=["user_input"], template=prompt_template)66    llm_chain = LLMChain(llm=get_llm(), prompt=prompt)67    68    return llm_chain69 70# Button to generate QR code71if st.button("Generate QR Code"):72    if user_input:  # Check if user input is provided73        agent_chain = create_agent_chain()74        _ = agent_chain.run(user_input)  # Call the agent chain, but ignore the response75        76        # Generate and display QR code77        qr_image = generate_qr_code(user_input)78        st.image(qr_image, caption='Generated QR Code', use_column_width=True)79    else:80        st.warning("Please enter some content to generate a QR code.")81 82 83