ionosphere commited on
Commit
4c8c543
β€’
1 Parent(s): 835ee70

Add api key in env for user, change model to small

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -7,45 +7,49 @@ from llama_index.embeddings import MistralAIEmbedding
7
  from llama_index import ServiceContext
8
  from llama_index.query_engine import RetrieverQueryEngine
9
 
10
-
11
- description = "Example of an assistant with Gradio and Mistral AI via its API"
12
- placeholder = "Ask a question"
13
  placeholder_url = "Extract text from this url"
14
- placeholder_api_key = "API key"
 
 
 
15
 
16
  query_engine = None
17
 
18
  with gr.Blocks() as demo:
19
 
20
  gr.Markdown(""" ### Welcome to Gaia Level 2 Demo
21
- Add an URL and your API key at the bottom of the interface before interacting with the Chat. This demo allows you to interact with a webpage and then ask questions to Mistral APIs. Mistral will answer with the context extracted from the webpage.
 
 
22
  """)
23
  chatbot = gr.Chatbot()
24
  msg = gr.Textbox()
25
  clear = gr.ClearButton([msg, chatbot])
26
 
27
- with gr.Row():
28
- api_key_text_box = gr.Textbox(placeholder=placeholder_api_key, container=False, scale=7)
29
 
30
- def setup_with_url(url, api_key):
31
  global query_engine
 
32
  # Set-up clients
33
- llm = MistralAI(api_key=api_key,model="mistral-medium")
34
- embed_model = MistralAIEmbedding(model_name='mistral-embed', api_key=api_key)
35
  service_context = ServiceContext.from_defaults(chunk_size=1024, llm=llm, embed_model=embed_model)
36
 
37
  # Set-up db
38
  documents = SimpleWebPageReader(html_to_text=True).load_data([url])
39
  index = VectorStoreIndex.from_documents(documents, service_context=service_context)
40
  query_engine = index.as_query_engine(similarity_top_k=15)
41
- return "I'm ready, please add a question here."
42
 
43
  with gr.Row():
44
  url_msg = gr.Textbox(placeholder=placeholder_url, container=False, scale=7)
45
  url_btn = gr.Button(value="Set-up API and process url βœ…", interactive=True)
46
- url_btn.click(setup_with_url, [url_msg, api_key_text_box], msg, show_progress= "full")
47
-
48
-
49
 
50
  def respond(message, chat_history):
51
  response = query_engine.query(message)
@@ -54,4 +58,6 @@ with gr.Blocks() as demo:
54
 
55
  msg.submit(respond, [msg, chatbot], [chatbot])
56
 
 
 
57
  demo.launch()
 
7
  from llama_index import ServiceContext
8
  from llama_index.query_engine import RetrieverQueryEngine
9
 
10
+ title = "Gaia Mistral Chat RAG URL Demo"
11
+ description = "Example of an assistant with Gradio, RAG from url and Mistral AI via its API"
12
+ placeholder = "Posez moi une question sur l'agriculture"
13
  placeholder_url = "Extract text from this url"
14
+ llm_model = 'mistral-small'
15
+ # choose api_key from .env or from input field
16
+ # placeholder_api_key = "API key"
17
+ env_api_key = os.environ.get("MISTRAL_API_KEY")
18
 
19
  query_engine = None
20
 
21
  with gr.Blocks() as demo:
22
 
23
  gr.Markdown(""" ### Welcome to Gaia Level 2 Demo
24
+ Add an URL at the bottom of the interface before interacting with the Chat.
25
+ This demo allows you to interact with a webpage and then ask questions to Mistral APIs.
26
+ Mistral will answer with the context extracted from the webpage.
27
  """)
28
  chatbot = gr.Chatbot()
29
  msg = gr.Textbox()
30
  clear = gr.ClearButton([msg, chatbot])
31
 
32
+ # with gr.Row():
33
+ # api_key_text_box = gr.Textbox(placeholder=placeholder_api_key, container=False, scale=7)
34
 
35
+ def setup_with_url(url):
36
  global query_engine
37
+
38
  # Set-up clients
39
+ llm = MistralAI(api_key=env_api_key,model=llm_model)
40
+ embed_model = MistralAIEmbedding(model_name='mistral-embed', api_key=env_api_key)
41
  service_context = ServiceContext.from_defaults(chunk_size=1024, llm=llm, embed_model=embed_model)
42
 
43
  # Set-up db
44
  documents = SimpleWebPageReader(html_to_text=True).load_data([url])
45
  index = VectorStoreIndex.from_documents(documents, service_context=service_context)
46
  query_engine = index.as_query_engine(similarity_top_k=15)
47
+ return placeholder
48
 
49
  with gr.Row():
50
  url_msg = gr.Textbox(placeholder=placeholder_url, container=False, scale=7)
51
  url_btn = gr.Button(value="Set-up API and process url βœ…", interactive=True)
52
+ url_btn.click(setup_with_url, [url_msg], msg, show_progress= "full")
 
 
53
 
54
  def respond(message, chat_history):
55
  response = query_engine.query(message)
 
58
 
59
  msg.submit(respond, [msg, chatbot], [chatbot])
60
 
61
+ demo.title = title
62
+
63
  demo.launch()