SharvNey commited on
Commit
1dd3a75
·
verified ·
1 Parent(s): a66a75b

Create backend.py

Browse files
Files changed (1) hide show
  1. backend.py +61 -0
backend.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # ---- TOKEN ----
5
+ HF_TOKEN = os.getenv("HF_TOKEN")
6
+ if not HF_TOKEN:
7
+ raise RuntimeError("HF_TOKEN not found. Add it in Space → Settings → Repository secrets")
8
+
9
+ # ---- MODEL ----
10
+ MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
11
+ client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
12
+
13
+
14
+ def run_llm(prompt: str) -> str:
15
+ """Run prompt using conversational API (supported by this model)."""
16
+ try:
17
+ messages = [{"role": "user", "content": prompt}]
18
+ resp = client.chat_completion(
19
+ messages=messages,
20
+ max_tokens=300,
21
+ temperature=0.7,
22
+ )
23
+ return resp.choices[0].message["content"].strip()
24
+ except Exception as e:
25
+ # raise clean error (no stacktrace in UI)
26
+ raise RuntimeError(f"LLM call failed: {e}")
27
+
28
+
29
+ def generate_variants(base_prompt: str):
30
+ meta_prompt = f"""
31
+ Rewrite the following prompt in 5 improved ways:
32
+ 1. More specific
33
+ 2. More technical
34
+ 3. More concise
35
+ 4. More creative
36
+ 5. Step-by-step style
37
+
38
+ Return ONLY the rewritten prompts, one per line.
39
+
40
+ Original prompt:
41
+ {base_prompt}
42
+ """
43
+ text = run_llm(meta_prompt)
44
+
45
+ variants = []
46
+ for line in text.split("\n"):
47
+ line = line.strip("-• ").strip()
48
+ if line:
49
+ variants.append(line)
50
+
51
+ # Fallback if parsing fails
52
+ if len(variants) < 3:
53
+ variants = [
54
+ f"You are an expert. {base_prompt}",
55
+ f"Explain step by step: {base_prompt}",
56
+ f"Give a concise answer: {base_prompt}",
57
+ f"Provide a technical explanation: {base_prompt}",
58
+ f"Explain creatively: {base_prompt}",
59
+ ]
60
+
61
+ return variants