llamalogic / app.py
sherrybabe1978's picture
Update app.py
7a968c2 verified
raw
history blame
3.65 kB
import streamlit as st
import groq
import os
import json
import time
# Initialize the Groq client with the API key from Hugging Face secrets
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
st.error("No Groq API key found. Please add your GROQ_API_KEY to the Hugging Face Space secrets.")
st.stop()
client = groq.Groq(api_key=api_key)
def make_api_call(messages, max_tokens, is_final_answer=False):
for attempt in range(3):
try:
response = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=messages,
max_tokens=max_tokens,
temperature=0.2,
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
except Exception as e:
if attempt == 2:
if is_final_answer:
return {"title": "Error", "content": f"Failed to generate final answer after 3 attempts. Error: {str(e)}"}
else:
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
time.sleep(1) # Wait for 1 second before retrying
# ... [rest of the code remains the same] ...
def main():
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
st.title("g1: Using Llama-3.1 70b on Groq to create o1-like reasoning chains")
st.markdown("""
This is an early prototype of using prompting to create o1-like reasoning chains to improve output accuracy. It is not perfect and accuracy has yet to be formally evaluated. It is powered by Groq so that the reasoning step is fast!
Open source [repository here](https://github.com/bklieger-groq)
""")
if not api_key:
st.warning("Please add your GROQ_API_KEY to the Hugging Face Space secrets to use this application.")
st.markdown("""
To add your API key:
1. Go to the Settings tab of this Space
2. Scroll down to the "Repository secrets" section
3. Click on "New secret"
4. Set the secret name as `GROQ_API_KEY`
5. Paste your Groq API key as the value
6. Click "Add secret"
7. Rebuild the Space
""")
st.stop()
# Text input for user query
user_query = st.text_input("Enter your query:", placeholder="e.g., How many 'R's are in the word strawberry?")
if user_query:
st.write("Generating response...")
# Create empty elements to hold the generated text and total time
response_container = st.empty()
time_container = st.empty()
# Generate and display the response
for steps, total_thinking_time in generate_response(user_query):
with response_container.container():
for i, (title, content, thinking_time) in enumerate(steps):
if title.startswith("Final Answer"):
st.markdown(f"### {title}")
st.markdown(content.replace('\n', '<br>'), unsafe_allow_html=True)
else:
with st.expander(title, expanded=True):
st.markdown(content.replace('\n', '<br>'), unsafe_allow_html=True)
# Only show total time when it's available at the end
if total_thinking_time is not None:
time_container.markdown(f"**Total thinking time: {total_thinking_time:.2f} seconds**")
if __name__ == "__main__":
main()