Spaces:
Running
Running
Create app.py
Browse filesv.0.1
- add chat with chosen LLM Model,
- add Conversation History,
- add Dataset uploader
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
+
from langchain import HuggingFacePipeline
|
5 |
+
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessage
|
6 |
+
from langchain_core.messages import SystemMessage
|
7 |
+
import nltk
|
8 |
+
import json
|
9 |
+
import pandas as pd
|
10 |
+
|
11 |
+
# Download nltk stopwords
|
12 |
+
nltk.download('stopwords')
|
13 |
+
|
14 |
+
# Function to load the conversation history
|
15 |
+
def load_conversation_history(file):
|
16 |
+
with open(file, 'r') as f:
|
17 |
+
return json.load(f)
|
18 |
+
|
19 |
+
# Function to save the conversation history
|
20 |
+
def save_conversation_history(history, file):
|
21 |
+
with open(file, 'w') as f:
|
22 |
+
json.dump(history, f)
|
23 |
+
|
24 |
+
# Initialize conversation history
|
25 |
+
conversation_history = []
|
26 |
+
if st.session_state.get('conversation_history'):
|
27 |
+
conversation_history = st.session_state.conversation_history
|
28 |
+
|
29 |
+
# Title
|
30 |
+
st.title('Culture AI v.0.1')
|
31 |
+
|
32 |
+
# Model selection
|
33 |
+
model_name = st.selectbox('Choose a model:', [
|
34 |
+
'mistralai/Mistral-7B-Instruct-v0.1',
|
35 |
+
'meta-llama/Meta-Llama-3–8B',
|
36 |
+
'microsoft/Phi-3-mini-4k-instruct',
|
37 |
+
'microsoft/phi-1_5',
|
38 |
+
'speakleash/Bielik-11B-v2.3-Instruct'
|
39 |
+
# Add more models as needed
|
40 |
+
])
|
41 |
+
|
42 |
+
# Upload dataset
|
43 |
+
dataset_file = st.file_uploader('Upload your dataset (CSV format)', type='csv')
|
44 |
+
if dataset_file:
|
45 |
+
df = pd.read_csv(dataset_file)
|
46 |
+
|
47 |
+
# Initialize tokenizer and model
|
48 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
49 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
50 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, return_full_text=True)
|
51 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
52 |
+
|
53 |
+
# Chat interface
|
54 |
+
st.write('## Chat')
|
55 |
+
topic = st.text_input('Enter a topic for the conversation:', 'Machine Learning')
|
56 |
+
prompt = ChatPromptTemplate.from_messages([
|
57 |
+
SystemMessage(content=f"Write a response related to the input topic in one paragraph"),
|
58 |
+
HumanMessagePromptTemplate.from_template("```{topic}```"),
|
59 |
+
])
|
60 |
+
|
61 |
+
chain = prompt | llm
|
62 |
+
|
63 |
+
if st.button('Generate Response'):
|
64 |
+
output = chain.invoke({"topic": topic})
|
65 |
+
st.write(output.content)
|
66 |
+
conversation_history.append({"user": topic, "assistant": output.content})
|
67 |
+
|
68 |
+
# Save conversation history
|
69 |
+
if st.button('Save Conversation History'):
|
70 |
+
save_conversation_history(conversation_history, 'conversation_history.json')
|
71 |
+
st.success('Conversation history saved!')
|
72 |
+
|
73 |
+
# Display conversation history
|
74 |
+
st.write('## Conversation History')
|
75 |
+
st.write(conversation_history)
|
76 |
+
|
77 |
+
# Update session state for conversation history
|
78 |
+
st.session_state.conversation_history = conversation_history
|