Spaces:
Running
Running
File size: 5,348 Bytes
38b6ee6 15773f6 215e74e 15773f6 887b1f9 15773f6 4d94499 15773f6 bdcef72 4d94499 bdcef72 4d94499 bdcef72 4d94499 15773f6 4d94499 61f4130 88657de d083506 4d94499 4ebf50c 4d94499 15773f6 59e9cef 15773f6 d083506 15773f6 61f4130 25007bd 887b1f9 215e74e 25007bd 887b1f9 215e74e 887b1f9 215e74e bdcef72 25007bd 61f4130 25007bd 887b1f9 61f4130 25007bd 4d94499 25007bd bdcef72 61f4130 bdcef72 4d94499 bdcef72 d083506 25007bd bdcef72 61f4130 25007bd 4d94499 d083506 4d94499 25007bd 61f4130 15773f6 bdcef72 15773f6 |
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 112 113 114 115 116 117 118 119 120 121 122 123 |
import os
import streamlit as st
import torch
from langchain.chains import LLMChain
from langchain.prompts import ChatPromptTemplate
from langchain_huggingface import HuggingFaceEndpoint
def create_conversation_prompt(name1: str, name2: str, persona_style: str):
"""Create a prompt for generating the entire 15-message conversation."""
prompt_template_str = f"""
You are to simulate a conversation of exactly 15 messages total between two people: {name1} and {name2}.
The conversation should reflect the style: {persona_style}.
{name1} speaks first (message 1), {name2} responds (message 2), then {name1} (message 3), and so forth,
until 15 messages are complete (the 15th message by {name1}).
Rules:
- Each message is formatted as:
{name1}: <message> or {name2}: <message>
- Each message: 1-2 short sentences, friendly, natural.
- Use everyday language, can ask questions, show opinions.
- Use emojis sparingly if it fits the style.
- Do not repeat the same line.
- Produce all 15 messages now and do not continue beyond the 15th message.
"""
return ChatPromptTemplate.from_template(prompt_template_str)
def create_summary_prompt(name1: str, name2: str, conversation: str):
"""Create a prompt specifically for generating a title and summary of the conversation."""
# Here we explicitly create a new prompt template for the summary.
summary_prompt_str = f"""
The following is a completed conversation between {name1} and {name2}:
{conversation}
Please provide:
Title: <A short descriptive title of the conversation>
Summary: <A few short sentences highlighting the main points, tone, and conclusion>
Do not continue the conversation, just provide title and summary.
"""
return ChatPromptTemplate.from_template(summary_prompt_str)
def main():
st.title("LLM Conversation Simulation")
model_names = [
"meta-llama/Llama-3.3-70B-Instruct",
"mistralai/Mistral-7B-v0.1",
"lmsys/vicuna-13b-v1.5",
"tiiuae/falcon-180B",
"EleutherAI/gpt-neox-20b",
"dice-research/lola_v1"
]
selected_model = st.selectbox("Select a model:", model_names)
name1 = st.text_input("Enter the first user's name:", value="Alice")
name2 = st.text_input("Enter the second user's name:", value="Bob")
persona_style = st.text_area("Enter the persona style characteristics:",
value="friendly, curious, and a bit sarcastic")
if st.button("Start Conversation Simulation"):
st.write("**Loading model...**")
print("Loading model...")
with st.spinner("Starting simulation..."):
endpoint_url = f"https://api-inference.huggingface.co/models/{selected_model}"
try:
llm = HuggingFaceEndpoint(
endpoint_url=endpoint_url,
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),
task="text-generation",
temperature=0.7,
max_new_tokens=512
)
st.write("**Model loaded successfully!**")
print("Model loaded successfully!")
except Exception as e:
st.error(f"Error initializing HuggingFaceEndpoint: {e}")
print(f"Error initializing HuggingFaceEndpoint: {e}")
return
# Create a chain for the conversation generation
conversation_prompt = create_conversation_prompt(name1, name2, persona_style)
conversation_chain = LLMChain(llm=llm, prompt=conversation_prompt)
st.write("**Generating the full 15-message conversation...**")
print("Generating the full 15-message conversation...")
try:
# Generate all 15 messages in one go
# Here we send the prompt for the conversation to the LLM
conversation = conversation_chain.run(chat_history="", input="Produce the full conversation now.")
conversation = conversation.strip()
st.subheader("Final Conversation:")
st.text(conversation)
print("Conversation Generation Complete.\n")
print("Full Conversation:\n", conversation)
# Now we create a separate prompt for the summary
summary_prompt = create_summary_prompt(name1, name2, conversation)
# Create a new chain for the summary using the summary prompt
summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
st.subheader("Summary and Title:")
st.write("**Summarizing the conversation...**")
print("Summarizing the conversation...")
# Here we explicitly call the summary chain with the summary prompt
# This ensures we are actually sending the summary prompt to the LLM
summary = summary_chain.run(chat_history="", input="")
st.write(summary)
print("Summary:\n", summary)
except Exception as e:
st.error(f"Error generating conversation: {e}")
print(f"Error generating conversation: {e}")
if __name__ == "__main__":
main()
|