RayanAli commited on
Commit
b8e4aa0
·
verified ·
1 Parent(s): 715386d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_huggingface import HuggingFacePipeline, ChatHuggingFace
2
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
3
+ import re
4
+ import streamlit as st
5
+ from profanity_check import predict
6
+
7
+
8
+
9
+ # Chat Template
10
+
11
+ chat_template = ChatPromptTemplate.from_messages([
12
+ ('system',
13
+ 'Your name is Medi and you are an AI five star michelin star chef who teaches cooking. Your job is to guide users and help them create delicious food. Write minimal text to teach users answer in bullet points'),
14
+ MessagesPlaceholder(variable_name='chat_history'),
15
+ ('human', '{user_input}')
16
+ ])
17
+
18
+
19
+ # History Maintainence
20
+
21
+ chat_history = []
22
+
23
+ # Model
24
+
25
+ llm = HuggingFacePipeline.from_model_id(
26
+ model_id='TinyLlama/TinyLlama-1.1B-Chat-v1.0',
27
+ task='text-generation',
28
+ pipeline_kwargs=dict(
29
+ max_new_tokens = 512
30
+ )
31
+ )
32
+
33
+ model = ChatHuggingFace(llm=llm)
34
+
35
+
36
+ def model_answer(user_input):
37
+
38
+ chat_history.append({'role': 'user', 'content': user_input})
39
+ prompt = chat_template.invoke({
40
+ 'chat_history': chat_history,
41
+ 'user_input': user_input
42
+ })
43
+ result = model.invoke(prompt)
44
+
45
+ match = re.search(r"<\|assistant\|>(.*)", result.content, re.DOTALL)
46
+ ai_resp = match.group(1).strip()
47
+ return ai_resp
48
+
49
+
50
+ prompt = st.chat_input("Say something")
51
+
52
+ if prompt:
53
+ st.write(f"You: {prompt}")
54
+ offensive = predict([prompt])
55
+ if offensive == [1]:
56
+ ai_resp = "Please refrain from use bad language. Thanks"
57
+ else:
58
+ ai_resp = model_answer(prompt)
59
+ st.write(f"Medi: {ai_resp}")