reddmann007 commited on
Commit
305964c
·
verified ·
1 Parent(s): 6e1c7f8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain_huggingface import HuggingFaceEndpoint
4
+ from langchain.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(repo_id=repo_id, temperature=0.7)
21
+
22
+ # --- 3. Sidebar Selection ---
23
+ option = st.sidebar.selectbox(
24
+ "Choose Prompting Technique",
25
+ ("Zero-Shot", "Single-Shot", "Few-Shot", "Chain of Thought")
26
+ )
27
+
28
+ user_input = st.text_input("Enter your query:", "How does a solar panel work?")
29
+
30
+ # --- 4. Logic for each Technique ---
31
+ if st.button("Generate Response"):
32
+ if option == "Zero-Shot":
33
+ prompt = PromptTemplate.from_template("{input}")
34
+ result = llm.invoke(prompt.format(input=user_input))
35
+
36
+ elif option == "Single-Shot" or option == "Few-Shot":
37
+ # Examples for sentiment analysis or translation
38
+ examples = [
39
+ {"input": "I am happy", "output": "Positive"},
40
+ {"input": "This is bad", "output": "Negative"}
41
+ ]
42
+ # Use only one example for single-shot
43
+ ex_to_use = examples[:1] if option == "Single-Shot" else examples
44
+
45
+ example_prompt = PromptTemplate(input_variables=["input", "output"], template="Input: {input}\nOutput: {output}")
46
+ few_shot_prompt = FewShotPromptTemplate(
47
+ examples=ex_to_use,
48
+ example_prompt=example_prompt,
49
+ suffix="Input: {input}\nOutput:",
50
+ input_variables=["input"]
51
+ )
52
+ result = llm.invoke(few_shot_prompt.format(input=user_input))
53
+
54
+ elif option == "Chain of Thought":
55
+ cot_template = """
56
+ Question: If John has 5 pears and gives 2 to Mary, how many does he have?
57
+ Answer: Let's think step by step. John starts with 5. He gives 2 away. 5 - 2 = 3. The answer is 3.
58
+
59
+ Question: {input}
60
+ Answer: Let's think step by step."""
61
+ prompt = PromptTemplate.from_template(cot_template)
62
+ result = llm.invoke(prompt.format(input=user_input))
63
+
64
+ st.subheader(f"Result ({option})")
65
+ st.write(result)