Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Step 1: Exchange API Key for Access Token
|
| 5 |
+
apikey = "aSSAcZP0GaAaMVb_3VMB7_69eG5bEP_MzmIQBm_TrwmG" # Replace with your new IBM Cloud API key
|
| 6 |
+
auth_url = "https://us-south.ml.cloud.ibm.com"
|
| 7 |
+
|
| 8 |
+
auth_headers = {
|
| 9 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 10 |
+
"Accept": "application/json"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
auth_data = {
|
| 14 |
+
"grant_type": "urn:ibm:params:oauth:grant-type:apikey",
|
| 15 |
+
"apikey": apikey
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
auth_response = requests.post(auth_url, headers=auth_headers, data=auth_data)
|
| 19 |
+
|
| 20 |
+
if auth_response.status_code == 200:
|
| 21 |
+
access_token = auth_response.json()["access_token"]
|
| 22 |
+
else:
|
| 23 |
+
st.error(f"Failed to retrieve access token: {auth_response.text}")
|
| 24 |
+
st.stop()
|
| 25 |
+
|
| 26 |
+
# Title of the Streamlit app
|
| 27 |
+
st.title("IBM Watson Text Generation with Hugging Face Integration")
|
| 28 |
+
|
| 29 |
+
# User input for the text generation prompt
|
| 30 |
+
user_input = st.text_area("Enter your prompt:", value="You are Granite Chat, an AI language model developed by IBM. You are a cautious assistant.")
|
| 31 |
+
|
| 32 |
+
# IBM Watson API request parameters
|
| 33 |
+
url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29"
|
| 34 |
+
body = {
|
| 35 |
+
"input": f"""<|system|>{user_input}<|assistant|>""",
|
| 36 |
+
"parameters": {
|
| 37 |
+
"decoding_method": "sample",
|
| 38 |
+
"max_new_tokens": 900,
|
| 39 |
+
"temperature": 0.7,
|
| 40 |
+
"top_k": 50,
|
| 41 |
+
"top_p": 1,
|
| 42 |
+
"repetition_penalty": 1.05
|
| 43 |
+
},
|
| 44 |
+
"model_id": "ibm-granite/granite-34b-code-instruct",
|
| 45 |
+
"project_id": "73c61b5e-83ae-4158-8ad6-8d3c8089146a"
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
headers = {
|
| 49 |
+
"Accept": "application/json",
|
| 50 |
+
"Content-Type": "application/json",
|
| 51 |
+
"Authorization": f"Bearer {access_token}"
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Step 2: Generate text using IBM Watson when the button is clicked
|
| 55 |
+
if st.button("Generate Text"):
|
| 56 |
+
response = requests.post(url, headers=headers, json=body)
|
| 57 |
+
|
| 58 |
+
if response.status_code == 200:
|
| 59 |
+
data = response.json()
|
| 60 |
+
|
| 61 |
+
# Accessing the 'generated_text' from the first result in 'results'
|
| 62 |
+
if 'results' in data and len(data['results']) > 0:
|
| 63 |
+
generated_text = data['results'][0]['generated_text']
|
| 64 |
+
st.write("**IBM Watson Output:**")
|
| 65 |
+
st.write(generated_text)
|
| 66 |
+
else:
|
| 67 |
+
st.error("No results found in the response.")
|
| 68 |
+
else:
|
| 69 |
+
st.error(f"Error {response.status_code}: {response.text}")
|