reddmann007 commited on
Commit
e2e02d1
·
verified ·
1 Parent(s): bc3fce8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -42
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="ShotCaller AI", page_icon="🤖")
9
- st.title("🤖 ShotCaller: Prompt Lab")
10
- st.markdown("Explore how different prompting techniques change AI reasoning.")
 
 
 
11
 
12
  # --- 2. Model Setup ---
13
  api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
14
 
15
  if not api_token:
16
- st.error("Missing HUGGINGFACEHUB_API_TOKEN. Add it to your Space Secrets!")
17
  st.stop()
18
 
19
- # Switching to Llama-3.1-8B-Instruct (Highly compatible)
20
- repo_id = "meta-llama/Llama-3.1-8B-Instruct"
21
 
22
  llm = HuggingFaceEndpoint(
23
  repo_id=repo_id,
24
- task="text-generation", # Base task
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
- "Select Prompting Strategy",
36
  ("Zero-Shot", "Single-Shot", "Few-Shot", "Chain of Thought")
37
  )
38
 
39
- user_query = st.text_input("What's your question?", "Why is the sky blue?")
 
 
 
 
 
 
40
 
41
- # --- 4. Logic for Prompting Techniques ---
42
- if st.button("Run Inference"):
 
 
 
43
  formatted_prompt = ""
44
 
45
  if option == "Zero-Shot":
46
- # Direct question, no guidance
47
- formatted_prompt = user_query
48
 
49
  elif option == "Single-Shot":
50
- # One example to set the tone/format
51
- formatted_prompt = f"Example: Input: Hello, Output: Hi there!\nInput: {user_query}\nOutput:"
 
 
 
 
 
 
52
 
53
  elif option == "Few-Shot":
54
- # Multiple examples for pattern recognition
55
- examples = [
56
- {"input": "The movie was great", "output": "Positive"},
57
- {"input": "The food was cold", "output": "Negative"},
58
- {"input": "It was an okay experience", "output": "Neutral"}
59
- ]
60
- example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
61
- few_shot_p = FewShotPromptTemplate(
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
- # Encouraging step-by-step logic
71
- formatted_prompt = f"Question: {user_query}\nAnswer: Let's think step by step."
 
 
 
 
 
 
 
72
 
73
- # Execute the call
74
- with st.spinner("Thinking..."):
75
  try:
76
- # Wrap our formatted string in a HumanMessage for the Chat Model
77
- messages = [HumanMessage(content=formatted_prompt)]
 
 
78
  response = chat_model.invoke(messages)
79
 
80
- st.subheader(f"Result: {option}")
81
- st.write(response.content)
 
 
 
 
 
82
  except Exception as e:
83
- st.error(f"An error occurred: {e}")
 
 
 
 
 
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")