tferhan commited on
Commit
10b332f
·
verified ·
1 Parent(s): 6edd6e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +207 -208
app.py CHANGED
@@ -1,208 +1,207 @@
1
- from langchain.prompts import StringPromptTemplate
2
- import re
3
- from document_scrapped import get_data
4
- import langchain
5
- from qa_txt import conversation_chain
6
- # from key_extract import chain
7
- from langchain_core.prompts import PromptTemplate
8
- from bs4 import BeautifulSoup
9
- import requests
10
- from data_process import *
11
- from langchain.tools.base import StructuredTool
12
- from langchain.agents import initialize_agent
13
- from qa_txt import llm
14
- from trans import trans
15
- import pathlib
16
- import gradio as gr
17
- from pydantic import BaseModel
18
- from fastapi import FastAPI
19
- from fastapi.middleware.cors import CORSMiddleware
20
-
21
- # import threading, time
22
- from langchain.agents import (
23
- create_react_agent,
24
- AgentExecutor,
25
- tool,
26
- )
27
- from langchain import hub
28
- import os
29
-
30
- app = FastAPI(title="Data Gov MA API", description="API for Data Gov MA", version="1.0.0")
31
-
32
-
33
- class Generate(BaseModel):
34
- text:str
35
-
36
-
37
- file_text = pathlib.Path('intents_v2.txt').read_text()
38
-
39
- prompt = hub.pull("hwchase17/react")
40
-
41
- def faq(query: str) -> str:
42
- reponse = conversation_chain.invoke({"input": query, 'document': file_text})
43
- return reponse
44
-
45
- qa_faq = StructuredTool.from_function(
46
- func = faq ,
47
- description="""
48
- Respond to general questions about the website like the documentation, contact, utility, support... Don't use it when the user request data about a subject (economie, justice, water, or any type of public dataset) only for contact or useful links data.
49
-
50
- Parameters :
51
- - query (string) : the same input as the user input no more no less and dont translate it even if it is in another language.
52
-
53
- Returns :
54
- - string : the output as returned from the function in french.
55
- """,
56
-
57
- )
58
-
59
- analyze_data = StructuredTool.from_function(
60
- func=get_data,
61
- description = """
62
- Analyze and summarize data from a given url asked by the user to summarize or analyze dont use it with the tool request_data and the user must write analyze before this function get executed.
63
- Parameters :
64
- - url (string) : the url given by the user.
65
- Returns :
66
- - string : Once the data is outputed from the function it get summarized and returned to the user and it must be in french.
67
- Example:
68
- >>> analyse this data of this link : https://data.gov.ma/data/fr/dataset/evolution-des-affaires-au-cours-du-quinquennat-2018-2022
69
- >>> input : https://data.gov.ma/data/fr/dataset/evolution-des-affaires-au-cours-du-quinquennat-2018-2022
70
- """,
71
- return_direct = True,
72
- )
73
-
74
- def request_data(query: str) -> str:
75
- mot_cle = nettoyer_string(query)
76
- mots = mot_cle.split()
77
- ui = mots[0]
78
- rg = chercher_data(ui)
79
- if len(rg[0]):
80
- reponse_final = format_reponse(rg)
81
- return reponse_final
82
- else:
83
- return query
84
-
85
- fetch_data = StructuredTool.from_function(
86
- func=request_data,
87
- description="""
88
- Request and fetch data using a search keyword.
89
- Parameters :
90
- - query (string) : A keyword in french about the subject of what to user is looking for, it must be always be in french and a noun if not convert it.For example if the user inputed "I want data about water" you need to input water in french which is "eau" same for other languages and the words translatted must be nouns not adjectives or verbs also the user may request data about an organization where you need to take just the main subject for example "Je veux les données de l'agence de développement digitale" you take just "développement".
91
- Returns :
92
- - string : the output as returned from the function in french , includes the link to all the data about the keyword along with an example.
93
- """,
94
- return_direct = True,
95
- )
96
- # def request_data(query: str) -> str:
97
- # request = chain.invoke({"input": query})
98
- # mot_cle = nettoyer_string(request)
99
- # mots = mot_cle.split()
100
- # ui = mots[0]
101
- # rg = chercher_data(ui)
102
- # if len(rg[0]):
103
- # reponse_final = format_reponse(rg)
104
- # return reponse_final
105
- # else:
106
- # return "Désolé, il semble que nous n'ayons pas de données correspondant à votre demande pour le moment. Avez-vous une autre question ou avez-vous besoin d'aide sur quelque chose d'autre?"
107
-
108
- # fetch_data = StructuredTool.from_function(
109
- # func=request_data,
110
- # description="""
111
- # Request and fetch data using a search keyword.
112
-
113
- # Parameters :
114
- # - query (string) : the same input as the user input no more no less and always it must be in french if it isn't already. For example : "give me data about health" the input is health in french which is santé, same for other languages and the words translatted must be nouns not adjectives or verbs also the user may request data about an organization where you need to take just the main subject for example "Je veux les données de l'agence de développement digitale" you take just "développement".
115
- # Returns :
116
- # - string : the output as returned from the function in french , includes the link to all the data about the keyword along with an example.
117
- # """,
118
- # )
119
-
120
- def translate(query: str) -> str:
121
- translated = trans.invoke({"input": query})
122
- return translated
123
-
124
- translate_text = StructuredTool.from_function(
125
- func=translate,
126
- description= """
127
- Translate from any language to french. Don't use it if the text is already in french. Use it only with the function request_data don't use it with the other tools
128
-
129
- Parameters :
130
- - query (string) : the same input as the user input no more no less only if it isnt in french already.
131
- Returns :
132
- - string : isolate just the translated text in french with no other useless words.
133
- """,
134
- )
135
-
136
-
137
-
138
- # template = '''Answer the following questions as best you can. You have access to the following tools:
139
-
140
- # {tools}
141
-
142
- # Use the following format:
143
-
144
- # Question: the input question you must answer and must be in french if not translate it in french
145
- # Thought: you should always think about what to do
146
- # Action: the action to take, should be one of [{tool_names}]
147
- # Action Input: the input to the action
148
- # Observation: the result of the action, don't include /nObservation in the end of each observation just what you observed
149
- # ... (this Thought/Action/Action Input/Observation can repeat N times)
150
- # Thought: I now know the final answer
151
- # Final Answer: the final answer to the original input question and must always be in french no matter what.
152
-
153
- # Begin!
154
-
155
- # Question: {input}
156
- # Thought:{agent_scratchpad}'''
157
-
158
- # prompt_2 = PromptTemplate.from_template(template)
159
-
160
- tools_add = [
161
- qa_faq,
162
- fetch_data,
163
- analyze_data,
164
- translate_text,
165
- ]
166
-
167
- agent = create_react_agent(llm=llm, tools=tools_add, prompt=prompt)
168
-
169
- agent_executor = AgentExecutor(
170
- agent=agent,
171
- tools=tools_add,
172
- verbose=True,
173
- max_iterations = 10,
174
- handle_parsing_errors=True,
175
- #max_execution_time = 45, optionel mais useful dans le deployement
176
-
177
- )
178
-
179
- def data_gov_ma(message, history = []):
180
- try:
181
- response = agent_executor.invoke({"input": message})
182
- final_response = response['output']
183
- timeout_iteration_error = 'Agent stopped due to iteration limit or time limit.'
184
- if final_response == timeout_iteration_error:
185
- return Generate(text="Je suis désolé, je n'ai pas compris votre question.Pourriez-vous la reformuler s'il vous plaît ?")
186
- else:
187
- return Generate(text=final_response)
188
- except ValueError as e:
189
- return Generate(text="Je suis désolé, je n'ai pas compris votre question.Pourriez-vous la reformuler s'il vous plaît ?")
190
-
191
- app.add_middleware(
192
- CORSMiddleware,
193
- allow_origins=["*"],
194
- allow_credentials=True,
195
- allow_methods=["*"],
196
- allow_headers=["*"],
197
- )
198
-
199
- @app.get("/", tags=["Home"])
200
- def api_home():
201
- return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
202
-
203
- @app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
204
- def inference(input_prompt: str):
205
- return data_gov_ma(message=input_prompt)
206
-
207
-
208
-
 
1
+ from langchain.prompts import StringPromptTemplate
2
+ import re
3
+ from document_scrapped import get_data
4
+ import langchain
5
+ from qa_txt import conversation_chain
6
+ # from key_extract import chain
7
+ from langchain_core.prompts import PromptTemplate
8
+ from bs4 import BeautifulSoup
9
+ import requests
10
+ from data_process import *
11
+ from langchain.tools.base import StructuredTool
12
+ from langchain.agents import initialize_agent
13
+ from qa_txt import llm
14
+ from trans import trans
15
+ import pathlib
16
+ from pydantic import BaseModel
17
+ from fastapi import FastAPI
18
+ from fastapi.middleware.cors import CORSMiddleware
19
+
20
+ # import threading, time
21
+ from langchain.agents import (
22
+ create_react_agent,
23
+ AgentExecutor,
24
+ tool,
25
+ )
26
+ from langchain import hub
27
+ import os
28
+
29
+ app = FastAPI(title="Data Gov MA API", description="API for Data Gov MA", version="1.0.0")
30
+
31
+
32
+ class Generate(BaseModel):
33
+ text:str
34
+
35
+
36
+ file_text = pathlib.Path('intents_v2.txt').read_text()
37
+
38
+ prompt = hub.pull("hwchase17/react")
39
+
40
+ def faq(query: str) -> str:
41
+ reponse = conversation_chain.invoke({"input": query, 'document': file_text})
42
+ return reponse
43
+
44
+ qa_faq = StructuredTool.from_function(
45
+ func = faq ,
46
+ description="""
47
+ Respond to general questions about the website like the documentation, contact, utility, support... Don't use it when the user request data about a subject (economie, justice, water, or any type of public dataset) only for contact or useful links data.
48
+
49
+ Parameters :
50
+ - query (string) : the same input as the user input no more no less and dont translate it even if it is in another language.
51
+
52
+ Returns :
53
+ - string : the output as returned from the function in french.
54
+ """,
55
+
56
+ )
57
+
58
+ analyze_data = StructuredTool.from_function(
59
+ func=get_data,
60
+ description = """
61
+ Analyze and summarize data from a given url asked by the user to summarize or analyze dont use it with the tool request_data and the user must write analyze before this function get executed.
62
+ Parameters :
63
+ - url (string) : the url given by the user.
64
+ Returns :
65
+ - string : Once the data is outputed from the function it get summarized and returned to the user and it must be in french.
66
+ Example:
67
+ >>> analyse this data of this link : https://data.gov.ma/data/fr/dataset/evolution-des-affaires-au-cours-du-quinquennat-2018-2022
68
+ >>> input : https://data.gov.ma/data/fr/dataset/evolution-des-affaires-au-cours-du-quinquennat-2018-2022
69
+ """,
70
+ return_direct = True,
71
+ )
72
+
73
+ def request_data(query: str) -> str:
74
+ mot_cle = nettoyer_string(query)
75
+ mots = mot_cle.split()
76
+ ui = mots[0]
77
+ rg = chercher_data(ui)
78
+ if len(rg[0]):
79
+ reponse_final = format_reponse(rg)
80
+ return reponse_final
81
+ else:
82
+ return query
83
+
84
+ fetch_data = StructuredTool.from_function(
85
+ func=request_data,
86
+ description="""
87
+ Request and fetch data using a search keyword.
88
+ Parameters :
89
+ - query (string) : A keyword in french about the subject of what to user is looking for, it must be always be in french and a noun if not convert it.For example if the user inputed "I want data about water" you need to input water in french which is "eau" same for other languages and the words translatted must be nouns not adjectives or verbs also the user may request data about an organization where you need to take just the main subject for example "Je veux les données de l'agence de développement digitale" you take just "développement".
90
+ Returns :
91
+ - string : the output as returned from the function in french , includes the link to all the data about the keyword along with an example.
92
+ """,
93
+ return_direct = True,
94
+ )
95
+ # def request_data(query: str) -> str:
96
+ # request = chain.invoke({"input": query})
97
+ # mot_cle = nettoyer_string(request)
98
+ # mots = mot_cle.split()
99
+ # ui = mots[0]
100
+ # rg = chercher_data(ui)
101
+ # if len(rg[0]):
102
+ # reponse_final = format_reponse(rg)
103
+ # return reponse_final
104
+ # else:
105
+ # return "Désolé, il semble que nous n'ayons pas de données correspondant à votre demande pour le moment. Avez-vous une autre question ou avez-vous besoin d'aide sur quelque chose d'autre?"
106
+
107
+ # fetch_data = StructuredTool.from_function(
108
+ # func=request_data,
109
+ # description="""
110
+ # Request and fetch data using a search keyword.
111
+
112
+ # Parameters :
113
+ # - query (string) : the same input as the user input no more no less and always it must be in french if it isn't already. For example : "give me data about health" the input is health in french which is santé, same for other languages and the words translatted must be nouns not adjectives or verbs also the user may request data about an organization where you need to take just the main subject for example "Je veux les données de l'agence de développement digitale" you take just "développement".
114
+ # Returns :
115
+ # - string : the output as returned from the function in french , includes the link to all the data about the keyword along with an example.
116
+ # """,
117
+ # )
118
+
119
+ def translate(query: str) -> str:
120
+ translated = trans.invoke({"input": query})
121
+ return translated
122
+
123
+ translate_text = StructuredTool.from_function(
124
+ func=translate,
125
+ description= """
126
+ Translate from any language to french. Don't use it if the text is already in french. Use it only with the function request_data don't use it with the other tools
127
+
128
+ Parameters :
129
+ - query (string) : the same input as the user input no more no less only if it isnt in french already.
130
+ Returns :
131
+ - string : isolate just the translated text in french with no other useless words.
132
+ """,
133
+ )
134
+
135
+
136
+
137
+ # template = '''Answer the following questions as best you can. You have access to the following tools:
138
+
139
+ # {tools}
140
+
141
+ # Use the following format:
142
+
143
+ # Question: the input question you must answer and must be in french if not translate it in french
144
+ # Thought: you should always think about what to do
145
+ # Action: the action to take, should be one of [{tool_names}]
146
+ # Action Input: the input to the action
147
+ # Observation: the result of the action, don't include /nObservation in the end of each observation just what you observed
148
+ # ... (this Thought/Action/Action Input/Observation can repeat N times)
149
+ # Thought: I now know the final answer
150
+ # Final Answer: the final answer to the original input question and must always be in french no matter what.
151
+
152
+ # Begin!
153
+
154
+ # Question: {input}
155
+ # Thought:{agent_scratchpad}'''
156
+
157
+ # prompt_2 = PromptTemplate.from_template(template)
158
+
159
+ tools_add = [
160
+ qa_faq,
161
+ fetch_data,
162
+ analyze_data,
163
+ translate_text,
164
+ ]
165
+
166
+ agent = create_react_agent(llm=llm, tools=tools_add, prompt=prompt)
167
+
168
+ agent_executor = AgentExecutor(
169
+ agent=agent,
170
+ tools=tools_add,
171
+ verbose=True,
172
+ max_iterations = 10,
173
+ handle_parsing_errors=True,
174
+ #max_execution_time = 45, optionel mais useful dans le deployement
175
+
176
+ )
177
+
178
+ def data_gov_ma(message, history = []):
179
+ try:
180
+ response = agent_executor.invoke({"input": message})
181
+ final_response = response['output']
182
+ timeout_iteration_error = 'Agent stopped due to iteration limit or time limit.'
183
+ if final_response == timeout_iteration_error:
184
+ return Generate(text="Je suis désolé, je n'ai pas compris votre question.Pourriez-vous la reformuler s'il vous plaît ?")
185
+ else:
186
+ return Generate(text=final_response)
187
+ except ValueError as e:
188
+ return Generate(text="Je suis désolé, je n'ai pas compris votre question.Pourriez-vous la reformuler s'il vous plaît ?")
189
+
190
+ app.add_middleware(
191
+ CORSMiddleware,
192
+ allow_origins=["*"],
193
+ allow_credentials=True,
194
+ allow_methods=["*"],
195
+ allow_headers=["*"],
196
+ )
197
+
198
+ @app.get("/", tags=["Home"])
199
+ def api_home():
200
+ return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
201
+
202
+ @app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
203
+ def inference(input_prompt: str):
204
+ return data_gov_ma(message=input_prompt)
205
+
206
+
207
+