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