Sharyar commited on
Commit
c0d3986
·
1 Parent(s): 3666e81

Refactor: Switch to google/flan-t5-xxl for reliability

Browse files
Files changed (1) hide show
  1. app.py +30 -39
app.py CHANGED
@@ -2,30 +2,28 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # Use a reliable zero-shot model from the Hugging Face Inference API
6
- MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"
7
 
8
- # It's recommended to set your Hugging Face token as an environment variable
9
- # For local testing, you can also pass it directly: client = InferenceClient(token="hf_...")
10
  client = InferenceClient(model=MODEL_ID)
11
 
12
- SYSTEM_PROMPT = """
13
- You are an expert AI assistant that rewrites software project descriptions.
14
- Your task is to convert a developer's informal, rough notes into a clear,
15
- concise, and professional technical summary.
16
-
17
- Key guidelines:
18
- - Preserve all core technical details and technologies mentioned.
19
- - Adopt a neutral, professional, and informative tone.
20
- - Correct grammar and improve the overall structure and clarity.
21
- - Aim for a length of 2 to 4 well-structured sentences.
22
- - Do not repeat the original input. Focus only on the improved version.
 
23
  """.strip()
24
 
25
  def improve_description(user_input: str) -> str:
26
  """
27
- Rewrites a rough project description into a professional, technical summary
28
- using a zero-shot large language model.
29
  """
30
  if not user_input or not user_input.strip():
31
  return "Please provide a description to improve."
@@ -34,31 +32,24 @@ def improve_description(user_input: str) -> str:
34
  if len(user_text) < 10:
35
  return "The description is too short. Please add more details about your project."
36
 
37
- # Construct the prompt for the model
38
- messages = [
39
- {"role": "system", "content": SYSTEM_PROMPT},
40
- {
41
- "role": "user",
42
- "content": (
43
- "Please rewrite the following project description into a professional "
44
- "and technical summary, following all system guidelines.\n\n"
45
- f"Original Description:\n'{user_text}'"
46
- ),
47
- },
48
- ]
49
 
50
  try:
51
- # Call the Inference API
52
- response = client.chat_completion(
53
- messages=messages,
54
- max_tokens=300,
55
- temperature=0.2, # Lower temperature for more focused and professional output
56
- top_p=0.9,
 
57
  )
58
- improved_text = response.choices[0].message.content.strip()
 
 
59
 
60
- # Final check to prevent returning the original text
61
- if improved_text.lower() == user_text.lower():
62
  return "Could not generate a meaningful improvement. Please try rephrasing or adding more detail."
63
 
64
  return improved_text
@@ -69,7 +60,7 @@ def improve_description(user_input: str) -> str:
69
  return "Sorry, the AI model service is currently unavailable. Please try again later."
70
 
71
 
72
- # --- Gradio Interface ---
73
  with gr.Blocks(title="Technical Description Assistant", theme=gr.themes.Soft()) as demo:
74
  gr.Markdown(
75
  """
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # Use a reliable and powerful text-to-text model from the Hugging Face Inference API
6
+ MODEL_ID = "google/flan-t5-xxl"
7
 
 
 
8
  client = InferenceClient(model=MODEL_ID)
9
 
10
+ # System prompt is less critical for non-instruct models, but we can use it in the user prompt.
11
+ REWRITE_TASK_PROMPT = """
12
+ Please rewrite the following rough project description into a clear, concise, and professional technical summary.
13
+ - Preserve all core technical details.
14
+ - Use a neutral and informative tone.
15
+ - Correct grammar and improve the structure.
16
+ - Do not repeat the original input.
17
+
18
+ Original Description:
19
+ '{user_text}'
20
+
21
+ Professional Summary:
22
  """.strip()
23
 
24
  def improve_description(user_input: str) -> str:
25
  """
26
+ Rewrites a rough project description using a zero-shot text-to-text model.
 
27
  """
28
  if not user_input or not user_input.strip():
29
  return "Please provide a description to improve."
 
32
  if len(user_text) < 10:
33
  return "The description is too short. Please add more details about your project."
34
 
35
+ # Construct the prompt for the text-to-text model
36
+ prompt = REWRITE_TASK_PROMPT.format(user_text=user_text)
 
 
 
 
 
 
 
 
 
 
37
 
38
  try:
39
+ # Use the `text_generation` method for non-chat models
40
+ improved_text = client.text_generation(
41
+ prompt,
42
+ max_new_tokens=300,
43
+ temperature=0.3,
44
+ top_p=0.95,
45
+ repetition_penalty=1.2,
46
  )
47
+
48
+ # The output is a single string, clean it up.
49
+ improved_text = improved_text.strip()
50
 
51
+ # Final check to prevent returning the original text or empty strings
52
+ if not improved_text or improved_text.lower() == user_text.lower():
53
  return "Could not generate a meaningful improvement. Please try rephrasing or adding more detail."
54
 
55
  return improved_text
 
60
  return "Sorry, the AI model service is currently unavailable. Please try again later."
61
 
62
 
63
+ # --- Gradio Interface (no changes needed here) ---
64
  with gr.Blocks(title="Technical Description Assistant", theme=gr.themes.Soft()) as demo:
65
  gr.Markdown(
66
  """