Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| import requests | |
| # Step 1: Load the API key and Project ID from environment variables | |
| apikey = os.getenv("IBM_API_KEY") | |
| project_id = os.getenv("IBM_PROJECT_ID") | |
| # Check if the API key and Project ID are set | |
| if not apikey or not project_id: | |
| st.error("API key or Project ID not found. Please set the environment variables.") | |
| st.stop() | |
| # Step 2: Exchange API Key for Access Token | |
| auth_url = "https://iam.cloud.ibm.com/identity/token" | |
| auth_headers = { | |
| "Content-Type": "application/x-www-form-urlencoded", | |
| "Accept": "application/json" | |
| } | |
| auth_data = { | |
| "grant_type": "urn:ibm:params:oauth:grant-type:apikey", | |
| "apikey": apikey | |
| } | |
| auth_response = requests.post(auth_url, headers=auth_headers, data=auth_data) | |
| if auth_response.status_code == 200: | |
| access_token = auth_response.json()["access_token"] | |
| else: | |
| st.error(f"Failed to retrieve access token: {auth_response.text}") | |
| st.stop() | |
| # Step 3: Streamlit Title and User Input | |
| st.title("IBM Watson Text Generation with Hugging Face Integration") | |
| 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.") | |
| # Step 4: Define the IBM Watson API Request | |
| url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29" | |
| body = { | |
| "input": f"""<|system|>{user_input}<|assistant|>""", | |
| "parameters": { | |
| "decoding_method": "sample", | |
| "max_new_tokens": 900, | |
| "temperature": 0.7, | |
| "top_k": 50, | |
| "top_p": 1, | |
| "repetition_penalty": 1.05 | |
| }, | |
| "model_id": "ibm/granite-13b-chat-v2", | |
| "project_id": project_id | |
| } | |
| headers = { | |
| "Accept": "application/json", | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {access_token}" | |
| } | |
| # Step 5: Generate text using IBM Watson when the button is clicked | |
| if st.button("Generate Text"): | |
| response = requests.post(url, headers=headers, json=body) | |
| if response.status_code == 200: | |
| data = response.json() | |
| # Accessing the 'generated_text' from the first result in 'results' | |
| if 'results' in data and len(data['results']) > 0: | |
| generated_text = data['results'][0]['generated_text'] | |
| st.write("**IBM Watson Output:**") | |
| st.write(generated_text) | |
| else: | |
| st.error("No results found in the response.") | |
| else: | |
| st.error(f"Error {response.status_code}: {response.text}") | |