Spaces:
Sleeping
Sleeping
Shubham 10000 commited on
Commit Β·
d8f8858
1
Parent(s): 441070d
first commit
Browse files- src/streamlit_app.py +12 -9
src/streamlit_app.py
CHANGED
|
@@ -2,10 +2,14 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import sqlite3
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# --- DATABASE SETUP ---
|
| 7 |
def init_db():
|
| 8 |
-
conn = sqlite3.connect(
|
| 9 |
c = conn.cursor()
|
| 10 |
c.execute('''CREATE TABLE IF NOT EXISTS history (
|
| 11 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -17,14 +21,14 @@ def init_db():
|
|
| 17 |
conn.close()
|
| 18 |
|
| 19 |
def save_feedback(user_text, feedback, tone):
|
| 20 |
-
conn = sqlite3.connect(
|
| 21 |
c = conn.cursor()
|
| 22 |
c.execute("INSERT INTO history (text, feedback, tone) VALUES (?, ?, ?)", (user_text, feedback, tone))
|
| 23 |
conn.commit()
|
| 24 |
conn.close()
|
| 25 |
|
| 26 |
def load_history():
|
| 27 |
-
conn = sqlite3.connect(
|
| 28 |
c = conn.cursor()
|
| 29 |
c.execute("SELECT * FROM history ORDER BY id DESC LIMIT 10")
|
| 30 |
data = c.fetchall()
|
|
@@ -34,7 +38,7 @@ def load_history():
|
|
| 34 |
# --- API CONFIG ---
|
| 35 |
st.set_page_config(page_title="AI Grammarly + Tone Analyzer", page_icon="βοΈ")
|
| 36 |
st.title("βοΈ AI Writing Assistant + Tone Analyzer")
|
| 37 |
-
st.write("Improve your writing and see its tone (formal, casual, persuasive, emotional) using Groq LLaMA models.")
|
| 38 |
|
| 39 |
# --- API KEY ---
|
| 40 |
api_key = None
|
|
@@ -52,8 +56,10 @@ if not api_key:
|
|
| 52 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 53 |
HEADERS = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 54 |
|
|
|
|
| 55 |
init_db()
|
| 56 |
|
|
|
|
| 57 |
user_input = st.text_area("π Enter your text:", height=200, placeholder="Type or paste your paragraph here...")
|
| 58 |
|
| 59 |
# --- FUNCTION TO CALL GROQ ---
|
|
@@ -71,7 +77,7 @@ def analyze_text(text):
|
|
| 71 |
Text: {text}
|
| 72 |
"""
|
| 73 |
payload = {
|
| 74 |
-
"model": "llama-3.1-8b-instant",
|
| 75 |
"messages": [
|
| 76 |
{"role": "system", "content": "You are a professional writing assistant that improves text and detects tone."},
|
| 77 |
{"role": "user", "content": prompt}
|
|
@@ -82,12 +88,9 @@ def analyze_text(text):
|
|
| 82 |
response.raise_for_status()
|
| 83 |
content = response.json()["choices"][0]["message"]["content"]
|
| 84 |
try:
|
| 85 |
-
result = response.json()["choices"][0]["message"]["content"]
|
| 86 |
-
# Ensure JSON parsing
|
| 87 |
-
import json
|
| 88 |
parsed = json.loads(content)
|
| 89 |
return parsed
|
| 90 |
-
except:
|
| 91 |
return {"feedback": content, "tone": "Unknown"}
|
| 92 |
|
| 93 |
# --- ANALYZE BUTTON ---
|
|
|
|
| 2 |
import requests
|
| 3 |
import sqlite3
|
| 4 |
import os
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# --- DATABASE PATH ---
|
| 8 |
+
DB_PATH = "/tmp/writing_assistant.db" # β
Hugging Face writable folder
|
| 9 |
|
| 10 |
# --- DATABASE SETUP ---
|
| 11 |
def init_db():
|
| 12 |
+
conn = sqlite3.connect(DB_PATH)
|
| 13 |
c = conn.cursor()
|
| 14 |
c.execute('''CREATE TABLE IF NOT EXISTS history (
|
| 15 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 21 |
conn.close()
|
| 22 |
|
| 23 |
def save_feedback(user_text, feedback, tone):
|
| 24 |
+
conn = sqlite3.connect(DB_PATH)
|
| 25 |
c = conn.cursor()
|
| 26 |
c.execute("INSERT INTO history (text, feedback, tone) VALUES (?, ?, ?)", (user_text, feedback, tone))
|
| 27 |
conn.commit()
|
| 28 |
conn.close()
|
| 29 |
|
| 30 |
def load_history():
|
| 31 |
+
conn = sqlite3.connect(DB_PATH)
|
| 32 |
c = conn.cursor()
|
| 33 |
c.execute("SELECT * FROM history ORDER BY id DESC LIMIT 10")
|
| 34 |
data = c.fetchall()
|
|
|
|
| 38 |
# --- API CONFIG ---
|
| 39 |
st.set_page_config(page_title="AI Grammarly + Tone Analyzer", page_icon="βοΈ")
|
| 40 |
st.title("βοΈ AI Writing Assistant + Tone Analyzer")
|
| 41 |
+
st.write("Improve your writing and see its tone (formal, casual, persuasive, emotional, or neutral) using Groq LLaMA models.")
|
| 42 |
|
| 43 |
# --- API KEY ---
|
| 44 |
api_key = None
|
|
|
|
| 56 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 57 |
HEADERS = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 58 |
|
| 59 |
+
# --- INIT DATABASE ---
|
| 60 |
init_db()
|
| 61 |
|
| 62 |
+
# --- USER INPUT ---
|
| 63 |
user_input = st.text_area("π Enter your text:", height=200, placeholder="Type or paste your paragraph here...")
|
| 64 |
|
| 65 |
# --- FUNCTION TO CALL GROQ ---
|
|
|
|
| 77 |
Text: {text}
|
| 78 |
"""
|
| 79 |
payload = {
|
| 80 |
+
"model": "llama-3.1-8b-instant", # β
Active Groq model
|
| 81 |
"messages": [
|
| 82 |
{"role": "system", "content": "You are a professional writing assistant that improves text and detects tone."},
|
| 83 |
{"role": "user", "content": prompt}
|
|
|
|
| 88 |
response.raise_for_status()
|
| 89 |
content = response.json()["choices"][0]["message"]["content"]
|
| 90 |
try:
|
|
|
|
|
|
|
|
|
|
| 91 |
parsed = json.loads(content)
|
| 92 |
return parsed
|
| 93 |
+
except json.JSONDecodeError:
|
| 94 |
return {"feedback": content, "tone": "Unknown"}
|
| 95 |
|
| 96 |
# --- ANALYZE BUTTON ---
|