Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain import PromptTemplate, OpenAI, LLMChain
|
3 |
+
from langchain.chains import RetrievalAugmentedGenerationChain
|
4 |
+
from langchain.tools import GoogleCustomSearchAPIWrapper
|
5 |
+
from groq import GroqClient
|
6 |
+
|
7 |
+
# Initialize Groq client
|
8 |
+
groq_client = GroqClient(api_key='your_groq_api_key')
|
9 |
+
|
10 |
+
# Define function to extract keywords using Groq
|
11 |
+
def extract_keywords(query):
|
12 |
+
response = groq_client.keywords(query)
|
13 |
+
keywords = response['keywords']
|
14 |
+
return keywords
|
15 |
+
|
16 |
+
# Define function to search on noticiasjuridicas.es
|
17 |
+
def search_noticiasjuridicas(keywords):
|
18 |
+
search_query = "site:www.noticiasjuridicas.es " + " ".join(keywords)
|
19 |
+
search_tool = GoogleCustomSearchAPIWrapper(api_key="your_google_api_key", search_engine_id="your_search_engine_id")
|
20 |
+
results = search_tool(search_query)
|
21 |
+
return results
|
22 |
+
|
23 |
+
# Define function to generate response using retrieved context
|
24 |
+
def generate_response(query, context):
|
25 |
+
template = """Based on the following information:
|
26 |
+
{context}
|
27 |
+
|
28 |
+
Here is the response to your query:
|
29 |
+
{query}
|
30 |
+
|
31 |
+
Response:
|
32 |
+
"""
|
33 |
+
prompt_template = PromptTemplate(template=template, input_variables=["context", "query"])
|
34 |
+
llm = OpenAI(model="gpt-4", api_key="your_openai_api_key")
|
35 |
+
chain = LLMChain(prompt=prompt_template, llm=llm)
|
36 |
+
response = chain.run({"context": context, "query": query})
|
37 |
+
return response
|
38 |
+
|
39 |
+
# Define the main function for the chatbot
|
40 |
+
def chatbot(query):
|
41 |
+
keywords = extract_keywords(query)
|
42 |
+
search_results = search_noticiasjuridicas(keywords)
|
43 |
+
context = "\n".join([result['snippet'] for result in search_results['items']])
|
44 |
+
response = generate_response(query, context)
|
45 |
+
return response
|
46 |
+
|
47 |
+
# Create Gradio interface
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=chatbot,
|
50 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your legal query here..."),
|
51 |
+
outputs="text",
|
52 |
+
title="Legal Assistant Chatbot",
|
53 |
+
description="Ask any legal questions and get answers based on the latest information from noticiasjuridicas.es"
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the interface
|
57 |
+
iface.launch()
|