NassimeBejaia commited on
Commit
96a6ad0
·
verified ·
1 Parent(s): 74fe6ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py CHANGED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
+
5
+ # Get token from Space secrets
6
+ API_TOKEN = os.getenv("HF_TOKEN")
7
+ client = InferenceClient(token=API_TOKEN)
8
+
9
+ # Initialize session state for chat history
10
+ if "chat_history" not in st.session_state:
11
+ st.session_state.chat_history = []
12
+ if "corrected_sentence" not in st.session_state:
13
+ st.session_state.corrected_sentence = ""
14
+
15
+ # Title of the app
16
+ st.title("Sentence Improver & Chat App")
17
+
18
+ # --- Sentence Correction Section ---
19
+ st.subheader("Improve a Sentence")
20
+ user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.")
21
+
22
+ if st.button("Improve Sentence"):
23
+ if user_input:
24
+ prompt = f"Correct and improve this sentence: '{user_input}'"
25
+ try:
26
+ response = client.text_generation(
27
+ prompt,
28
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1", # Or your working model
29
+ max_new_tokens=100,
30
+ temperature=0.7,
31
+ )
32
+ st.session_state.corrected_sentence = response.strip()
33
+ st.success(f"Improved Sentence: {st.session_state.corrected_sentence}")
34
+ except Exception as e:
35
+ st.error(f"Error: {str(e)}")
36
+ else:
37
+ st.warning("Please enter a sentence first!")
38
+
39
+ # --- Chat Section ---
40
+ st.subheader("Chat About the Corrected Sentence")
41
+ if st.session_state.corrected_sentence:
42
+ # Display chat history
43
+ for speaker, message in st.session_state.chat_history:
44
+ st.write(f"**{speaker}:** {message}")
45
+
46
+ # Chat input
47
+ chat_input = st.text_input("Ask something about the corrected sentence:", key="chat_input")
48
+ if st.button("Send"):
49
+ if chat_input:
50
+ # Build prompt with corrected sentence as context
51
+ prompt = (
52
+ f"The corrected sentence is: '{st.session_state.corrected_sentence}'. "
53
+ f"User asks: '{chat_input}'. Respond naturally."
54
+ )
55
+ try:
56
+ response = client.text_generation(
57
+ prompt,
58
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1", # Same model
59
+ max_new_tokens=150,
60
+ temperature=0.7,
61
+ )
62
+ # Add to chat history
63
+ st.session_state.chat_history.append(("You", chat_input))
64
+ st.session_state.chat_history.append(("LLM", response.strip()))
65
+ # Rerun to refresh the display
66
+ st.rerun()
67
+ except Exception as e:
68
+ st.error(f"Error in chat: {str(e)}")
69
+ else:
70
+ st.warning("Please enter a question!")
71
+ else:
72
+ st.write("Improve a sentence first to start chatting!")