reddmann007 commited on
Commit
ef59653
·
verified ·
1 Parent(s): fd0ce62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -34
app.py CHANGED
@@ -1,69 +1,84 @@
1
  import streamlit as st
2
  import os
3
- from langchain_huggingface import HuggingFaceEndpoint
4
  from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
 
5
 
6
  # --- 1. UI Setup ---
7
- st.set_page_config(page_title="Prompting Demo", page_icon="🤖")
8
- st.title("Prompt Engineering Demo")
9
- st.markdown("Experiment with Zero-Shot, Few-Shot, and Chain of Thought.")
10
 
11
  # --- 2. Model Setup ---
12
- # On HF Spaces, you store keys in "Settings > Variables and Secrets"
13
  api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
14
 
15
  if not api_token:
16
- st.error("Please add your HUGGINGFACEHUB_API_TOKEN to Secrets.")
17
  st.stop()
18
 
 
 
 
19
  repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
20
  llm = HuggingFaceEndpoint(
21
  repo_id=repo_id,
22
- # Remove 'task' and let HF auto-detect
23
- huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"]
 
24
  )
25
 
 
 
 
26
  # --- 3. Sidebar Selection ---
27
  option = st.sidebar.selectbox(
28
- "Choose Prompting Technique",
29
  ("Zero-Shot", "Single-Shot", "Few-Shot", "Chain of Thought")
30
  )
31
 
32
- user_input = st.text_input("Enter your query:", "How does a solar panel work?")
 
 
 
 
33
 
34
- # --- 4. Logic for each Technique ---
35
- if st.button("Generate Response"):
36
  if option == "Zero-Shot":
37
- prompt = PromptTemplate.from_template("{input}")
38
- result = llm.invoke(prompt.format(input=user_input))
39
 
40
- elif option == "Single-Shot" or option == "Few-Shot":
41
- # Examples for sentiment analysis or translation
 
 
 
 
42
  examples = [
43
- {"input": "I am happy", "output": "Positive"},
44
- {"input": "This is bad", "output": "Negative"}
 
45
  ]
46
- # Use only one example for single-shot
47
- ex_to_use = examples[:1] if option == "Single-Shot" else examples
48
-
49
  example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
50
- few_shot_prompt = FewShotPromptTemplate(
51
- examples=ex_to_use,
52
  example_prompt=example_prompt,
53
  suffix="Input: {input}\nOutput:",
54
  input_variables=["input"]
55
  )
56
- result = llm.invoke(few_shot_prompt.format(input=user_input))
57
 
58
  elif option == "Chain of Thought":
59
- cot_template = """
60
- Question: If John has 5 pears and gives 2 to Mary, how many does he have?
61
- Answer: Let's think step by step. John starts with 5. He gives 2 away. 5 - 2 = 3. The answer is 3.
62
-
63
- Question: {input}
64
- Answer: Let's think step by step."""
65
- prompt = PromptTemplate.from_template(cot_template)
66
- result = llm.invoke(prompt.format(input=user_input))
67
 
68
- st.subheader(f"Result ({option})")
69
- st.write(result)
 
 
 
 
 
 
 
 
 
 
1
  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
+ # Retrieve your token from Hugging Face Settings > Secrets
14
  api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
15
 
16
  if not api_token:
17
+ st.error("Missing HUGGINGFACEHUB_API_TOKEN. Add it to your Space Secrets!")
18
  st.stop()
19
 
20
+ # Initialize the endpoint
21
+ # Note: task="text-generation" is required for the underlying class,
22
+ # but ChatHuggingFace will handle the conversational routing.
23
  repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
24
  llm = HuggingFaceEndpoint(
25
  repo_id=repo_id,
26
+ task="text-generation",
27
+ temperature=0.7,
28
+ huggingfacehub_api_token=api_token
29
  )
30
 
31
+ # Wrap the LLM in ChatHuggingFace to satisfy the 'conversational' requirement
32
+ chat_model = ChatHuggingFace(llm=llm)
33
+
34
  # --- 3. Sidebar Selection ---
35
  option = st.sidebar.selectbox(
36
+ "Select Prompting Strategy",
37
  ("Zero-Shot", "Single-Shot", "Few-Shot", "Chain of Thought")
38
  )
39
 
40
+ user_query = st.text_input("What's your question?", "Why is the sky blue?")
41
+
42
+ # --- 4. Logic for Prompting Techniques ---
43
+ if st.button("Run Inference"):
44
+ formatted_prompt = ""
45
 
 
 
46
  if option == "Zero-Shot":
47
+ # Direct question, no guidance
48
+ formatted_prompt = user_query
49
 
50
+ elif option == "Single-Shot":
51
+ # One example to set the tone/format
52
+ formatted_prompt = f"Example: Input: Hello, Output: Hi there!\nInput: {user_query}\nOutput:"
53
+
54
+ elif option == "Few-Shot":
55
+ # Multiple examples for pattern recognition
56
  examples = [
57
+ {"input": "The movie was great", "output": "Positive"},
58
+ {"input": "The food was cold", "output": "Negative"},
59
+ {"input": "It was an okay experience", "output": "Neutral"}
60
  ]
 
 
 
61
  example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
62
+ few_shot_p = FewShotPromptTemplate(
63
+ examples=examples,
64
  example_prompt=example_prompt,
65
  suffix="Input: {input}\nOutput:",
66
  input_variables=["input"]
67
  )
68
+ formatted_prompt = few_shot_p.format(input=user_query)
69
 
70
  elif option == "Chain of Thought":
71
+ # Encouraging step-by-step logic
72
+ formatted_prompt = f"Question: {user_query}\nAnswer: Let's think step by step."
 
 
 
 
 
 
73
 
74
+ # Execute the call
75
+ with st.spinner("Thinking..."):
76
+ try:
77
+ # Wrap our formatted string in a HumanMessage for the Chat Model
78
+ messages = [HumanMessage(content=formatted_prompt)]
79
+ response = chat_model.invoke(messages)
80
+
81
+ st.subheader(f"Result: {option}")
82
+ st.write(response.content)
83
+ except Exception as e:
84
+ st.error(f"An error occurred: {e}")