Spaces:
Sleeping
Sleeping
File size: 4,299 Bytes
305964c ef59653 09864f8 e2e02d1 305964c e2e02d1 305964c e2e02d1 305964c e2e02d1 bc3fce8 a11bf8e fd0ce62 e2e02d1 ef59653 e2e02d1 a11bf8e 305964c ef59653 e2e02d1 305964c e2e02d1 305964c e2e02d1 ef59653 e2e02d1 ef59653 305964c e2e02d1 305964c ef59653 e2e02d1 ef59653 e2e02d1 305964c e2e02d1 305964c e2e02d1 ef59653 e2e02d1 ef59653 e2e02d1 ef59653 e2e02d1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import streamlit as st
import os
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_core.messages import HumanMessage, SystemMessage
# --- 1. UI Setup ---
st.set_page_config(page_title="FlavorFeedback AI", page_icon="🍴")
st.title("🍴 FlavorFeedback: Prompting Lab")
st.markdown("""
This app demonstrates how different **Prompt Engineering** techniques affect AI performance
in a Restaurant Management context.
""")
# --- 2. Model Setup ---
api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
if not api_token:
st.error("Please add your HUGGINGFACEHUB_API_TOKEN to the Space Secrets.")
st.stop()
# Using Qwen 2.5 7B - Excellent at reasoning and ungated (no request needed)
repo_id = "Qwen/Qwen2.5-7B-Instruct"
llm = HuggingFaceEndpoint(
repo_id=repo_id,
task="text-generation",
temperature=0.7,
huggingfacehub_api_token=api_token
)
chat_model = ChatHuggingFace(llm=llm)
# --- 3. Sidebar & Logic Selection ---
st.sidebar.header("Configuration")
option = st.sidebar.selectbox(
"Choose Technique",
("Zero-Shot", "Single-Shot", "Few-Shot", "Chain of Thought")
)
# --- 4. Define Defaults for the Use Case ---
defaults = {
"Zero-Shot": "The pasta was okay, but the service was incredibly slow and the waiter forgot our drinks twice.",
"Single-Shot": "The staff was so friendly and the steak was cooked to perfection, though the decor felt a bit dated.",
"Few-Shot": "The music was way too loud and we couldn't hear each other at the table.",
"Chain of Thought": "Issue: Undercooked Salmon. Bill Total: $72. Resolution: Waiter apologized but kept the item on the bill."
}
user_query = st.text_area("Input Data / Review:", value=defaults[option], height=150)
# --- 5. Execution Logic ---
if st.button("Generate Response"):
system_instruction = "You are a professional Restaurant Operations Assistant."
formatted_prompt = ""
if option == "Zero-Shot":
formatted_prompt = f"Classify the following restaurant review as 'Positive', 'Negative', or 'Neutral':\n\nReview: {user_query}\n\nSentiment:"
elif option == "Single-Shot":
formatted_prompt = (
"Extract key ratings from the review.\n\n"
"Example:\n"
"Input: 'The pizza was amazing, but it was too loud in there.'\n"
"Output: Food: 5/5 | Service: N/A | Atmosphere: 2/5\n\n"
f"Input: '{user_query}'\n"
"Output:"
)
elif option == "Few-Shot":
formatted_prompt = (
"As the Manager, write a brief response to this feedback.\n\n"
"Example 1:\nFeedback: 'Best tacos in town!'\n"
"Response: Thank you so much! We're thrilled you enjoyed the tacos.\n\n"
"Example 2:\nFeedback: 'Wait time was too long.'\n"
"Response: We apologize for the delay. We are working on our speed.\n\n"
f"Feedback: {user_query}\n"
"Response:"
)
elif option == "Chain of Thought":
system_instruction = "You are a senior restaurant manager who follows strict logic rules."
formatted_prompt = (
"Rule 1: Complaint must involve Food Quality or Billing.\n"
"Rule 2: Total spend must be over $50.\n"
"Rule 3: Issue was not resolved on the spot.\n\n"
"Determine if this customer gets a 15% discount based on the feedback below.\n"
f"Feedback: {user_query}\n\n"
"Let's think step-by-step:"
)
with st.spinner("Analyzing..."):
try:
messages = [
SystemMessage(content=system_instruction),
HumanMessage(content=formatted_prompt)
]
response = chat_model.invoke(messages)
st.subheader(f"Results: {option}")
st.success(response.content)
# Show the "Internal Logic" for the portfolio
with st.expander("View the raw prompt sent to AI"):
st.code(formatted_prompt)
except Exception as e:
st.error(f"Error: {e}")
# --- 6. Footer ---
st.sidebar.markdown("---")
st.sidebar.info("Built with LangChain & Hugging Face") |