Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- llama3_qa_bot.py +65 -0
- llama3_qa_bot_requirements.txt +2 -0
llama3_qa_bot.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary packages
|
| 2 |
+
from ibm_watsonx_ai import Credentials
|
| 3 |
+
from ibm_watsonx_ai import APIClient
|
| 4 |
+
from ibm_watsonx_ai.foundation_models import Model, ModelInference
|
| 5 |
+
from ibm_watsonx_ai.foundation_models.schema import TextChatParameters
|
| 6 |
+
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
watsonx_API="L0sx3BXcQRWNmz45mbBLxL1UiZGnftHFQTwITAci-523"
|
| 10 |
+
project_id="ed8f7a2c-e597-4a09-a98f-dbdcef57a0d0"
|
| 11 |
+
|
| 12 |
+
# Set credentials to use the model
|
| 13 |
+
credentials = {
|
| 14 |
+
"url" : "https://au-syd.ml.cloud.ibm.com",
|
| 15 |
+
"apikey": watsonx_API
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Model and project settings
|
| 19 |
+
model_id = "meta-llama/llama-3-2-11b-vision-instruct" # Directly specifying the LLAMA3 model
|
| 20 |
+
project_id = project_id # Specifying project_id as provided
|
| 21 |
+
|
| 22 |
+
params = TextChatParameters(
|
| 23 |
+
temperature=0.1,
|
| 24 |
+
max_tokens=512
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Initialize the model
|
| 28 |
+
model = ModelInference(
|
| 29 |
+
model_id=model_id,
|
| 30 |
+
credentials=credentials,
|
| 31 |
+
project_id=project_id,
|
| 32 |
+
params=params
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Function to generate a response from the model
|
| 36 |
+
def generate_response(prompt_txt):
|
| 37 |
+
messages = [
|
| 38 |
+
{
|
| 39 |
+
"role": "user",
|
| 40 |
+
"content": [
|
| 41 |
+
{
|
| 42 |
+
"type": "text",
|
| 43 |
+
"text": prompt_txt
|
| 44 |
+
},
|
| 45 |
+
]
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
generated_response = model.chat(messages=messages)
|
| 49 |
+
generated_text = generated_response['choices'][0]['message']['content']
|
| 50 |
+
|
| 51 |
+
return generated_text
|
| 52 |
+
|
| 53 |
+
# Create Gradio interface
|
| 54 |
+
chat_application = gr.Interface(
|
| 55 |
+
fn=generate_response,
|
| 56 |
+
flagging_mode="never",
|
| 57 |
+
inputs=gr.Textbox(label="Input", lines=2, placeholder="Type your question here..."),
|
| 58 |
+
outputs=gr.Textbox(label="Output"),
|
| 59 |
+
title="Watsonx.ai Chatbot",
|
| 60 |
+
description="Ask any question and the chatbot will try to answer."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Launch the app
|
| 64 |
+
chat_application.launch(share=True)
|
| 65 |
+
|
llama3_qa_bot_requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install gradio ibm_watsonx_ai email-validator
|
| 2 |
+
pip install numpy pandas
|