File size: 2,932 Bytes
96a6ad0
 
 
 
 
 
 
 
1c06c0b
96a6ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c06c0b
 
 
 
 
 
 
 
 
 
 
 
96a6ad0
 
 
1c06c0b
96a6ad0
 
 
 
 
 
 
 
 
1c06c0b
96a6ad0
1c06c0b
 
96a6ad0
 
1c06c0b
96a6ad0
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
from huggingface_hub import InferenceClient
import os

# Get token from Space secrets
API_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(token=API_TOKEN)

# Initialize session state
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 = ""

# Title of the app
st.title("Sentence Improver & Chat App")

# --- Sentence Correction Section ---
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",  # Or your working model
                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!")

# --- Chat Section ---
st.subheader("Chat About the Corrected Sentence")
if st.session_state.corrected_sentence:
    # Display chat history
    for speaker, message in st.session_state.chat_history:
        st.write(f"**{speaker}:** {message}")

    # Chat input with Enter key submission
    chat_input = st.text_input(
        "Ask something about the corrected sentence (press Enter to send):",
        key="chat_input",
        value="",  # Clear after each submission
        on_change=lambda: submit_chat(),  # Trigger on Enter
    )

    # Function to handle chat submission
    def submit_chat():
        chat_text = st.session_state.chat_input
        if chat_text:
            # Build prompt with corrected sentence as context
            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",  # Same model
                    max_new_tokens=150,
                    temperature=0.7,
                )
                # Add to chat history
                st.session_state.chat_history.append(("You", chat_text))
                st.session_state.chat_history.append(("LLM", response.strip()))
                # Clear the input field by resetting the key
                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!")