YogavasanthCV / Gemini-model-Sentiment-Analysis-BigQuery

1 stars 0 forks source link

how solve this issue in ubuntu 22 locally #1

Open johnfelipe opened 1 month ago

johnfelipe commented 1 month ago
import streamlit as st
from google.cloud import bigquery
import pandas as pd
import google.generativeai as genai
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Configure the Google Gemini API key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

# Initialize BigQuery client
client = bigquery.Client()

# Function to load Google Gemini model and get improvement suggestions
def get_gemini_response(text):
    model = genai.GenerativeModel("gemini-pro")
    prompt = f"Based on the following negative feedback: '{text}', how can the agent improve to achieve a positive score? What are the steps to take for improvisation?"
    response = model.generate_content(prompt)
    return response.text

# Define the BigQuery SQL query
query = """
SELECT conversation_id, ml_generate_text_llm_result, text, prompt
FROM `support-agent-rating.sentiment_analysis_dataset.review_sentiment_analysis`
"""

# Fetch data from BigQuery
def fetch_data():
    query_job = client.query(query)  # Make an API request.
    return query_job.result().to_dataframe()

# Analyze negative sentiments and get improvement suggestions
def analyze_negative_sentiments(data):
    negative_data = data[data['ml_generate_text_llm_result'].str.contains("Negative", case=False, na=False)]
    suggestions = []

    for _, row in negative_data.iterrows():
        text = row['text']
        suggestion = get_gemini_response(text)
        suggestions.append({
            "Conversation ID": row['conversation_id'],
            "Text": text,
            "Suggestion": suggestion
        })

    return pd.DataFrame(suggestions)

# Streamlit app
st.title("Sentiment Analysis Dashboard")

# Fetch data
data = fetch_data()

# Login Page
st.sidebar.header("Login")
entered_conversation_id = st.sidebar.text_input("Enter Conversation ID")

# Validate Conversation ID
if entered_conversation_id in data['conversation_id'].values:
    st.header(f"Conversation Details for ID: {entered_conversation_id}")

    # Filter data based on entered conversation_id
    filtered_data = data[data['conversation_id'] == entered_conversation_id]

    if not filtered_data.empty:
        st.write(filtered_data)

        # Analyze and display improvement suggestions
        suggestions_df = analyze_negative_sentiments(filtered_data)
        if not suggestions_df.empty:
            st.subheader("Improvement Suggestions")
            st.write(suggestions_df)
        else:
            st.write("No negative sentiments found for this conversation.")
    else:
        st.write("No data available for the entered Conversation ID.")
else:
    st.write("Please enter a valid Conversation ID to view the details.")

i put this .env in root path

GOOGLE_APPLICATION_CREDENTIALS=/root/Gemini-model-Sentiment-Analysis-BigQuery/connect.json
GOOGLE_API_KEY=blablalblablalbla

show me this error in putty:

root@template:~/Gemini-model-Sentiment-Analysis-BigQuery# streamlit run stream.py

Collecting usage statistics. To deactivate, set browser.gatherUsageStats to false.

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://192.168.20.20:8501
  External URL: http://190.158.250.48:8501

2024-09-19 13:13:16.038 Uncaught app exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
  File "/usr/local/lib/python3.10/dist-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec
    exec(code, module.__dict__)
  File "/root/Gemini-model-Sentiment-Analysis-BigQuery/stream.py", line 5, in <module>
    client = bigquery.Client()
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/bigquery/client.py", line 241, in __init__
    super(Client, self).__init__(
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/client/__init__.py", line 320, in __init__
    _ClientProjectMixin.__init__(self, project=project, credentials=credentials)
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/client/__init__.py", line 268, in __init__
    project = self._determine_default(project)
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/client/__init__.py", line 287, in _determine_default
    return _determine_default_project(project)
  File "/usr/local/lib/python3.10/dist-packages/google/cloud/_helpers/__init__.py", line 152, in _determine_default_project
    _, project = google.auth.default()
  File "/usr/local/lib/python3.10/dist-packages/google/auth/_default.py", line 691, in default
    raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

how solve?

johnfelipe commented 1 month ago

anyone?