Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| from docx import Document | |
| import io | |
| def call_groq_llm(text, api_key): | |
| headers = { | |
| "Authorization": f"Bearer {api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| prompt = ( | |
| "You're a qualitative analysis expert. Analyze this social media caption with the following tasks:\n\n" | |
| "1. **Abstract**: Summarize the key message in 2-3 sentences.\n" | |
| "2. **Emotional Tone**: Describe the emotional tone (e.g., hopeful, angry, empowering).\n" | |
| "3. **Strategic Frames**: Identify and classify the strategic frames used (e.g., advocacy, call to action, intersectionality).\n" | |
| "4. **Codes**: Provide a list of 4-8 qualitative codes along with a one-line explanation for each code.\n\n" | |
| "Return the output in markdown format with clear section headers. Format codes like:\n" | |
| "- **CodeName**: One-line description of the code." | |
| ) | |
| payload = { | |
| "model": "llama-3.3-70b-versatile", | |
| "messages": [ | |
| {"role": "system", "content": prompt}, | |
| {"role": "user", "content": text} | |
| ] | |
| } | |
| response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=headers) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return f"Error: {response.status_code} - {response.text}" | |
| def create_docx_file(result_text): | |
| doc = Document() | |
| doc.add_heading('Groq LLM Analysis Result', 0) | |
| for line in result_text.split('\n'): | |
| if line.startswith('#'): | |
| doc.add_heading(line.replace('#', '').strip(), level=2) | |
| elif line.startswith("- **") and "**:" in line: | |
| # Add bullet point with bolded code and description | |
| code_line = line.strip().replace("**", "").split(":") | |
| doc.add_paragraph(f"• {code_line[0]}: {code_line[1].strip()}") | |
| else: | |
| doc.add_paragraph(line.strip()) | |
| byte_stream = io.BytesIO() | |
| doc.save(byte_stream) | |
| byte_stream.seek(0) | |
| return byte_stream | |
| def show_analysis_placeholder(text): | |
| st.subheader("🧠 Groq LLM Analysis") | |
| groq_api_key = st.secrets["GROQ_API_KEY"] | |
| if groq_api_key and st.button("Run Analysis"): | |
| result = call_groq_llm(text, groq_api_key) | |
| st.markdown("### 🧾 LLM Output") | |
| st.markdown(result) | |
| # Extract and display codes with descriptions | |
| st.markdown("### 🏷️ Extracted Codes with Descriptions") | |
| for line in result.split("\n"): | |
| if line.startswith("- **") and "**:" in line: | |
| # Parsing line like: - **Empowerment**: Expresses agency and strength | |
| line_clean = line.strip().lstrip("- ").replace("**", "") | |
| st.markdown(f"- {line_clean}") | |
| # Generate DOCX | |
| docx_file = create_docx_file(result) | |
| st.download_button( | |
| label="📥 Download Analysis as DOCX", | |
| data=docx_file, | |
| file_name="groq_analysis_result.docx", | |
| mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" | |
| ) | |