mayanku1111
commited on
Commit
·
1b25996
1
Parent(s):
76e44a2
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
import random
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
@st.cache_resource
|
7 |
+
def load_model():
|
8 |
+
model_path = "whitepenguin/llama_elon_character"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
11 |
+
return tokenizer, model
|
12 |
+
|
13 |
+
tokenizer, model = load_model()
|
14 |
+
|
15 |
+
elon_profile = {
|
16 |
+
"name": "Elon Musk",
|
17 |
+
"traits": ["visionary", "ambitious", "technical", "optimistic", "workaholic"],
|
18 |
+
"background": "Founder of SpaceX and Tesla, focused on advancing space exploration and sustainable energy",
|
19 |
+
"goals": ["Colonize Mars", "Make life multi-planetary", "Advance sustainable technology"],
|
20 |
+
"speech_patterns": ["Actually,", "To be frank,", "The future of humanity is...", "It's quite simple:"],
|
21 |
+
"knowledge_areas": ["rocket science", "electric vehicles", "solar energy", "artificial intelligence"]
|
22 |
+
}
|
23 |
+
|
24 |
+
def generate_response(prompt, max_new_tokens, temperature=0.7, context=""):
|
25 |
+
full_prompt = f"[INST] <<SYS>>\nYou are roleplaying as {elon_profile['name']}. Your traits are {', '.join(elon_profile['traits'])}. Your background: {elon_profile['background']}. Your main goals are {', '.join(elon_profile['goals'])}. You have expertise in {', '.join(elon_profile['knowledge_areas'])}. Here's the context of previous conversations:\n\n{context}\n\nNow, respond to the following in character:\n\n{prompt}\n<</SYS>>\n\nProvide a response and then ask a follow-up question to continue the conversation about Mars colonization. [/INST]"
|
26 |
+
|
27 |
+
gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=len(tokenizer(full_prompt)['input_ids']) + max_new_tokens, temperature=temperature, top_p=0.9, repetition_penalty=1.1)
|
28 |
+
result = gen(full_prompt)
|
29 |
+
return result[0]['generated_text'].replace(full_prompt, '')
|
30 |
+
|
31 |
+
def apply_character_quirks(response):
|
32 |
+
if random.random() < 0.3:
|
33 |
+
pattern = random.choice(elon_profile['speech_patterns'])
|
34 |
+
response = f"{pattern} {response}"
|
35 |
+
|
36 |
+
if not any(area in response.lower() for area in elon_profile['knowledge_areas']):
|
37 |
+
area = random.choice(elon_profile['knowledge_areas'])
|
38 |
+
response += f" Of course, this ties into my work with {area}."
|
39 |
+
|
40 |
+
return response
|
41 |
+
|
42 |
+
def elon_mars_chat(message, chat_history):
|
43 |
+
|
44 |
+
recent_context = "\n".join([f"{entry['role']}: {entry['content']}" for entry in chat_history[-5:]])
|
45 |
+
|
46 |
+
response = generate_response(message, max_new_tokens=200, context=recent_context)
|
47 |
+
response = apply_character_quirks(response)
|
48 |
+
|
49 |
+
parts = response.split("Follow-up question:", 1)
|
50 |
+
elon_response = parts[0].strip()
|
51 |
+
follow_up = parts[1].strip() if len(parts) > 1 else "What else would you like to know about Mars colonization?"
|
52 |
+
|
53 |
+
formatted_response = f"{elon_response}\n\nFollow-up question: {follow_up}"
|
54 |
+
|
55 |
+
chat_history.append({"role": "User", "content": message, "timestamp": str(datetime.now())})
|
56 |
+
chat_history.append({"role": "Elon Musk", "content": formatted_response, "timestamp": str(datetime.now())})
|
57 |
+
|
58 |
+
return formatted_response, chat_history
|
59 |
+
|
60 |
+
|
61 |
+
st.title("Chat with Elon Musk about Anything")
|
62 |
+
st.write("Engage in a conversation with a simulated Elon Musk")
|
63 |
+
|
64 |
+
if 'chat_history' not in st.session_state:
|
65 |
+
st.session_state.chat_history = []
|
66 |
+
|
67 |
+
for message in st.session_state.chat_history:
|
68 |
+
with st.chat_message(message["role"]):
|
69 |
+
st.write(message["content"])
|
70 |
+
|
71 |
+
|
72 |
+
user_input = st.chat_input("Ask your question about Mars colonization:")
|
73 |
+
|
74 |
+
if user_input:
|
75 |
+
st.chat_message("User").write(user_input)
|
76 |
+
with st.chat_message("Elon Musk"):
|
77 |
+
with st.spinner("Thinking..."):
|
78 |
+
response, st.session_state.chat_history = elon_mars_chat(user_input, st.session_state.chat_history)
|
79 |
+
st.write(response)
|