AbenzaFran commited on
Commit
d69b9bc
1 Parent(s): 3cdd4da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ from dotenv import load_dotenv
4
+ load_dotenv()
5
+
6
+ from langchain.agents.openai_assistant import OpenAIAssistantRunnable
7
+ from langchain.agents import AgentExecutor
8
+
9
+ from langchain.schema import HumanMessage, AIMessage
10
+
11
+ import gradio
12
+
13
+ api_key = os.getenv('OPENAI_API_KEY_DAVID')
14
+ extractor_agent = os.getenv('ASSISTANT_ID_DECASTRO')
15
+
16
+ extractor_llm = OpenAIAssistantRunnable(assistant_id=extractor_agent, api_key=api_key, as_agent=True)
17
+
18
+
19
+ def remove_citation(text):
20
+ # Define the regex pattern to match the citation format 【number†text】
21
+ pattern = r"【\d+†\w+】"
22
+ # Replace the pattern with an empty string
23
+ return re.sub(pattern, "📚", text)
24
+
25
+ def predict(message, history):
26
+ history_langchain_format = []
27
+ for human, ai in history:
28
+ history_langchain_format.append(HumanMessage(content=human))
29
+ history_langchain_format.append(AIMessage(content=ai))
30
+ history_langchain_format.append(HumanMessage(content=message))
31
+ gpt_response = extractor_llm.invoke({"content": message})
32
+ output = gpt_response.return_values["output"]
33
+ non_cited_output = remove_citation(output)
34
+ return non_cited_output
35
+
36
+ chat = gradio.ChatInterface(predict, title="DeCastro Bot", description="DeCastro whatsapp bot.")
37
+ chat.launch(share=True)