File size: 2,313 Bytes
54c99ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd
from typing import Dict

def format_table_for_llm(df: pd.DataFrame, name: str) -> str:
    return f"\n{name}:\n{df.to_string()}\n" if not df.empty else f"[{name}: No data]\n"

def build_narrative_prompt(parsed_data: Dict, tables: Dict, style: str) -> str:
    metadata = parsed_data["metadata"]
    stats = parsed_data["statistics"]
    interviewee_type = parsed_data["interviewee_type"]
    
    tables_text = "\n".join([format_table_for_llm(df, name) for name, df in tables.items()])
    
    return f"""Write an executive research report for {metadata['total_transcripts']} {interviewee_type.lower()} interviews.



DATA TABLES:

{tables_text}



STRUCTURE:

1. EXECUTIVE OVERVIEW (2-3 paragraphs): Context, sample, high-level findings

2. KEY FINDINGS (3-5 sections): Each with narrative + data + significance

3. PATTERNS & THEMES (2 paragraphs): Cross-cutting insights

4. RECOMMENDATIONS (3-5 bullets): Actionable next steps



Write professionally. Quantify everything. Be specific. Lead with insights."""

def call_lmstudio(prompt: str) -> str:
    import requests
    url = os.getenv("LM_STUDIO_URL", "http://192.168.1.245:1234")
    try:
        r = requests.post(f"{url}/v1/chat/completions", json={
            "messages": [{"role": "system", "content": "You are an expert research report writer."}, 
                        {"role": "user", "content": prompt}],
            "max_tokens": 2000, "temperature": 0.7
        }, timeout=180)
        return r.json()["choices"][0]["message"]["content"]
    except Exception as e:
        return f"[Error: {e}]"

def call_hf_api(prompt: str) -> str:
    from huggingface_hub import InferenceClient
    try:
        client = InferenceClient(token=os.getenv("HUGGINGFACE_TOKEN", ""))
        return client.text_generation(prompt, model="mistralai/Mixtral-8x7B-Instruct-v0.1", 
                                      max_new_tokens=2000, temperature=0.7)
    except Exception as e:
        return f"[Error: {e}]"

def generate_narrative(parsed_data: Dict, tables: Dict, style: str, llm_backend: str) -> str:
    prompt = build_narrative_prompt(parsed_data, tables, style)
    if llm_backend == "lmstudio":
        return call_lmstudio(prompt)
    else:
        return call_hf_api(prompt)