| import streamlit as st |
| from openai import OpenAI |
|
|
| |
| client = OpenAI( |
| base_url="https://integrate.api.nvidia.com/v1", |
| api_key="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" |
| ) |
|
|
| |
| st.title("Rhazel's ChatBot") |
|
|
| |
| topic = st.text_area("Enter your topic or initial text:") |
|
|
| |
| output_format = st.selectbox( |
| "Select output format:", |
| ["Story", "Poem", "Article", "Code"] |
| ) |
|
|
| |
| tone = st.selectbox( |
| "Select tone/style:", |
| ["Formal", "Informal", "Humorous", "Technical"] |
| ) |
|
|
| |
| length = st.slider("Text Length", 50, 500, 150) |
|
|
| |
| creativity = st.slider("Creativity Level", 0.1, 1.0, 0.7) |
|
|
| |
| num_responses = st.number_input("Number of Responses", 1, 5, 1, 1) |
|
|
| |
| creative_mode = st.checkbox("Enable Creative Mode") |
| fact_checking = st.checkbox("Enable Fact-Checking") |
|
|
| |
| if st.button('Generate'): |
| if topic: |
| with st.spinner('Generating text...'): |
| try: |
| |
| prompt = f"Generate a {output_format} on the topic '{topic}' with a {tone} tone, length of {length} words, creativity level of {creativity * 100}%." |
| |
| if creative_mode: |
| prompt += " Use creative language." |
| if fact_checking: |
| prompt += " Fact-check the information." |
|
|
| |
| all_responses = [] |
| for _ in range(num_responses): |
| |
| completion = client.chat.completions.create( |
| model="meta/llama-3.2-3b-instruct", |
| messages=[{"role": "user", "content": prompt}], |
| temperature=creativity, |
| top_p=0.7, |
| max_tokens=length, |
| stream=True |
| ) |
|
|
| |
| full_response = "" |
|
|
| |
| for chunk in completion: |
| if chunk.choices[0].delta.content is not None: |
| full_response += chunk.choices[0].delta.content |
|
|
| |
| all_responses.append(full_response) |
|
|
| |
| for idx, response in enumerate(all_responses): |
| st.subheader(f"Response {idx + 1}") |
| st.write(response) |
| |
| except Exception as e: |
| st.error(f"Error generating text: {str(e)}") |
| else: |
| st.error("Please enter a topic to generate text.") |
|
|