|
import streamlit as st |
|
from huggingface_hub import InferenceClient |
|
import os |
|
|
|
|
|
API_TOKEN = os.getenv("HF_TOKEN_Mistral") |
|
client = InferenceClient(token=API_TOKEN) |
|
|
|
|
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
if "corrected_sentence" not in st.session_state: |
|
st.session_state.corrected_sentence = "" |
|
|
|
|
|
st.title("Sentence Improver & Chat App") |
|
|
|
|
|
st.subheader("Improve a Sentence") |
|
user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.") |
|
|
|
if st.button("Improve Sentence"): |
|
if user_input: |
|
prompt = f"Correct and improve this sentence: '{user_input}'" |
|
try: |
|
response = client.text_generation( |
|
prompt, |
|
model="mistralai/Mixtral-8x7B-Instruct-v0.1", |
|
max_new_tokens=100, |
|
temperature=0.7, |
|
) |
|
st.session_state.corrected_sentence = response.strip() |
|
st.success(f"Improved Sentence: {st.session_state.corrected_sentence}") |
|
except Exception as e: |
|
st.error(f"Error: {str(e)}") |
|
else: |
|
st.warning("Please enter a sentence first!") |
|
|
|
|
|
st.subheader("Chat About the Corrected Sentence") |
|
if st.session_state.corrected_sentence: |
|
|
|
for speaker, message in st.session_state.chat_history: |
|
st.write(f"**{speaker}:** {message}") |
|
|
|
|
|
chat_input = st.text_input( |
|
"Ask something about the corrected sentence (press Enter to send):", |
|
key="chat_input", |
|
value="", |
|
on_change=lambda: submit_chat(), |
|
) |
|
|
|
|
|
def submit_chat(): |
|
chat_text = st.session_state.chat_input |
|
if chat_text: |
|
|
|
prompt = ( |
|
f"The corrected sentence is: '{st.session_state.corrected_sentence}'. " |
|
f"User asks: '{chat_text}'. Respond naturally." |
|
) |
|
try: |
|
response = client.text_generation( |
|
prompt, |
|
model="mistralai/Mixtral-8x7B-Instruct-v0.1", |
|
max_new_tokens=150, |
|
temperature=0.7, |
|
) |
|
|
|
st.session_state.chat_history.append(("You", chat_text)) |
|
st.session_state.chat_history.append(("LLM", response.strip())) |
|
|
|
st.session_state.chat_input = "" |
|
except Exception as e: |
|
st.error(f"Error in chat: {str(e)}") |
|
|
|
else: |
|
st.write("Improve a sentence first to start chatting!") |