Skip to content

Commit

Permalink
Adding the crtic
Browse files Browse the repository at this point in the history
  • Loading branch information
Soumya Snigdha Kundu authored and Soumya Snigdha Kundu committed Oct 5, 2024
1 parent 3e2536e commit b1111da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
Binary file modified __pycache__/utils.cpython-310.pyc
Binary file not shown.
20 changes: 1 addition & 19 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,9 @@
from utils import _generate_poem
from utils import _random_concept
from utils import _common_themes
from utils import _generate_text
from utils import _critique_poem
from utils import URLS

def _critique_poem(poem):
"""
Critique the poem using the language model.
"""
prompt = f"""
Evaluate the following poem and determine if the addition is good enough.
Respond only with "yes" or "no".
Poem:
{poem}
"""
try:
output = _generate_text(prompt) # Use your existing _generate_text function
return output.strip().lower() # Normalize the response to lowercase for comparison
except (ssl.SSLError, RequestException) as e:
st.error(f"Network error occurred: {str(e)}. Please try again.")
return None

def main():
"""
Main function.
Expand Down
30 changes: 26 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import os
import dotenv
import mistralai
import streamlit
import streamlit as st
from requests.exceptions import RequestException
import ssl


def _load_image_as_base64(file_path):
Expand All @@ -23,7 +25,7 @@ def _generate_poem(concept, theme, url):
"""
# Combine previous responses to create context, excluding None values
previous_context = "\n".join(
filter(None, streamlit.session_state.responses) # Only include non-None responses
filter(None, st.session_state.responses) # Only include non-None responses
)
return _caption_image(
f"""
Expand Down Expand Up @@ -137,6 +139,26 @@ def _caption_image(prompt: str, url: str, json: bool = False) -> str:

# -----------------------------------------------------------------------------

def _critique_poem(poem):
"""
Critique the poem using the language model.
"""
prompt = f"""
Evaluate the following poem and determine if the addition is good enough.
Respond only with "yes" or "no".
Poem:
{poem}
"""
try:
output = _generate_text(prompt) # Use your existing _generate_text function
return output.strip().lower() # Normalize the response to lowercase for comparison
except (ssl.SSLError, RequestException) as e:
st.error(f"Network error occurred: {str(e)}. Please try again.")
return None

# -----------------------------------------------------------------------------

dotenv.load_dotenv()
THEME_DEFAULT = 'heavy industry'
API_KEY_MISTRAL = os.getenv('API_KEY_MISTRAL')
Expand All @@ -151,5 +173,5 @@ def _caption_image(prompt: str, url: str, json: bool = False) -> str:
]

# Initialize session state for responses if it doesn't exist
if 'responses' not in streamlit.session_state:
streamlit.session_state.responses = [None] * len(URLS) # Initialize with None for each page
if 'responses' not in st.session_state:
st.session_state.responses = [None] * len(URLS) # Initialize with None for each page

0 comments on commit b1111da

Please sign in to comment.