Spaces:
Sleeping
Sleeping
Commit ·
7d325a7
1
Parent(s): f70f8ab
Renewed
Browse files
app.py
CHANGED
|
@@ -1,44 +1,92 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import random
|
| 3 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Initialize OpenAI client (replace with your actual key)
|
| 6 |
client = OpenAI(
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
)
|
| 10 |
|
| 11 |
-
st.title("Harry Potter Character Description")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
if st.button("Get Description"):
|
| 18 |
-
if character_name:
|
| 19 |
-
messages = [{"content": f'{{"fullName":"{character_name}"}}', "role": "tool", "tool_call_id": "chatcmpl-tool-c64c70cc341547abbf96bb98573cc3b9", "name": "describe_harry_potter_character"}]
|
| 20 |
else:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
top_p=0.7,
|
| 30 |
-
max_tokens=1024,
|
| 31 |
-
stream=True,
|
| 32 |
-
tools=[{"type": "function", "function": {"name": "describe_harry_potter_character", "description": "Returns information and images of Harry Potter characters.", "parameters": {"type": "object", "properties": {"name": {"type": "string", "enum": ["Harry James Potter", "Hermione Jean Granger", "Ron Weasley", "Fred Weasley", "George Weasley", "Bill Weasley", "Percy Weasley", "Charlie Weasley", "Ginny Weasley", "Molly Weasley", "Arthur Weasley", "Neville Longbottom", "Luna Lovegood", "Draco Malfoy", "Albus Percival Wulfric Brian Dumbledore", "Minerva McGonagall", "Remus Lupin", "Rubeus Hagrid", "Sirius Black", "Severus Snape", "Bellatrix Lestrange", "Lord Voldemort", "Cedric Diggory", "Nymphadora Tonks", "James Potter"], "description": "Name of the Harry Potter character"}}, "required": ["name"]}}}],
|
| 33 |
-
tool_choice="auto"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
full_response = "" # Accumulate the streamed response
|
| 37 |
-
for chunk in completion:
|
| 38 |
-
if chunk.choices[0].delta.content is not None:
|
| 39 |
-
full_response += chunk.choices[0].delta.content
|
| 40 |
-
st.write(chunk.choices[0].delta.content, unsafe_allow_html=False) # Display each chunk as it arrives
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# You can now use the full_response if needed after the loop
|
| 44 |
-
# st.write("Full Response:", full_response) #For debugging
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import re
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize OpenAI client (REPLACE with your actual key from environment variables or a secure config)
|
| 7 |
+
|
| 8 |
+
# IMPORTANT: Never hardcode API keys in your code, especially for sharing!
|
| 9 |
|
|
|
|
| 10 |
client = OpenAI(
|
| 11 |
+
|
| 12 |
+
base_url="https://integrate.api.nvidia.com/v1", # Assuming this is your correct base URL
|
| 13 |
+
|
| 14 |
+
api_key="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" # Replace with your actual key
|
| 15 |
+
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
+
st.title("Sorting Hat")
|
| 20 |
+
|
| 21 |
+
def get_essay_answer():
|
| 22 |
+
"""Gets the essay answer from the user."""
|
| 23 |
+
st.subheader("Answer the Question:")
|
| 24 |
+
essay = st.text_area("Describe what you would do if you encountered a lost and distressed first-year student who has lost their pet toad and is terrified of being late for Herbology.", height=200) # Increased height for essay
|
| 25 |
+
return essay
|
| 26 |
+
|
| 27 |
+
def get_sorted(essay):
|
| 28 |
+
"""Handles the sorting process using the OpenAI API."""
|
| 29 |
+
if essay: # Check if essay is not empty
|
| 30 |
+
output_area = st.empty()
|
| 31 |
+
|
| 32 |
+
messages = [{"role": "user", "content": f"""Pretend that you are the sorting hat in Harry Potter. Sort me into a house in Hogwarts based on my answer. Give me the house ONLY (one word: Gryffindor, Hufflepuff, Ravenclaw, Slytherin) on the first line. On the second line explain your reasoning for sorting me there. Here is my answer:
|
| 33 |
+
{essay}
|
| 34 |
+
"""}]
|
| 35 |
+
|
| 36 |
+
with st.spinner("Sorting..."):
|
| 37 |
+
try:
|
| 38 |
+
completion = client.chat.completions.create(
|
| 39 |
+
model="meta/llama-3.1-405b-instruct", # Your model
|
| 40 |
+
messages=messages,
|
| 41 |
+
temperature=0.7,
|
| 42 |
+
max_tokens=200,
|
| 43 |
+
stream=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
full_response = ""
|
| 47 |
+
message_placeholder = st.empty()
|
| 48 |
|
| 49 |
+
for chunk in completion:
|
| 50 |
+
if chunk.choices[0].delta and chunk.choices[0].delta.content:
|
| 51 |
+
full_response += chunk.choices[0].delta.content
|
| 52 |
+
message_placeholder.markdown(full_response, unsafe_allow_html=False)
|
| 53 |
+
elif chunk.choices[0].finish_reason == "stop":
|
| 54 |
+
break
|
| 55 |
+
|
| 56 |
+
house_match = re.search(r"(Gryffindor|Hufflepuff|Ravenclaw|Slytherin)", full_response, re.IGNORECASE)
|
| 57 |
+
|
| 58 |
+
if house_match:
|
| 59 |
+
house = house_match.group(0).strip()
|
| 60 |
+
try:
|
| 61 |
+
parts = full_response.split(house + '\n', 1)
|
| 62 |
+
if len(parts) > 1:
|
| 63 |
+
explanation = parts[1].strip()
|
| 64 |
+
else:
|
| 65 |
+
explanation = "No explanation provided."
|
| 66 |
+
except Exception as e:
|
| 67 |
+
explanation = f"Error extracting explanation: {e}"
|
| 68 |
+
|
| 69 |
+
gif_path = {
|
| 70 |
+
"Gryffindor": "https://i.pinimg.com/originals/52/46/eb/5246eba389dfc5722324b484975a012f.gif",
|
| 71 |
+
"Hufflepuff": "https://c.tenor.com/ThUF6Bm-GLgAAAAM/always-harry-potter.gif",
|
| 72 |
+
"Ravenclaw": "https://media.tenor.com/Sgm7TzrEQzAAAAAC/harry-potter-ravenclaw.gif",
|
| 73 |
+
"Slytherin": "https://media.giphy.com/media/A1xhQcogWZAIg/giphy.gif"
|
| 74 |
+
}.get(house, "default.gif")
|
| 75 |
+
|
| 76 |
+
output_area.image(gif_path, width=300)
|
| 77 |
+
|
| 78 |
+
else:
|
| 79 |
+
output_area.error("The Sorting Hat's response was not in the expected format.")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
output_area.error(f"An error occurred: {e}")
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
+
st.warning("Please provide an answer.")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Main app flow
|
| 89 |
+
essay = get_essay_answer() # Get the essay answer
|
| 90 |
+
|
| 91 |
+
if st.button("Get Sorted!"):
|
| 92 |
+
get_sorted(essay) # Pass the essay to get_sorted()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|