Spaces:
Runtime error
Runtime error
@panditu1
commited on
Commit
·
a2a4fbb
1
Parent(s):
85aa305
this is the best until now
Browse files
app.py
CHANGED
@@ -46,20 +46,42 @@ api_client = InferenceClient(
|
|
46 |
)
|
47 |
|
48 |
def generate_positive_thought():
|
49 |
-
|
50 |
try:
|
51 |
start_time = time.time()
|
|
|
52 |
completion = api_client.chat.completions.create(
|
53 |
model=API_MODEL_NAME,
|
54 |
messages=[
|
55 |
-
{
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
]
|
58 |
)
|
59 |
end_time = time.time()
|
60 |
response_time = round(end_time - start_time, 2)
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
except Exception as e:
|
64 |
traceback.print_exc()
|
65 |
return f"❌ API Call Failed:\n{type(e).__name__}: {str(e)}"
|
@@ -104,4 +126,4 @@ with gr.Blocks(title="Dual-Mode Mood App", theme=gr.themes.Soft()) as demo:
|
|
104 |
|
105 |
# Launch the app
|
106 |
if __name__ == "__main__":
|
107 |
-
demo.launch(share=
|
|
|
46 |
)
|
47 |
|
48 |
def generate_positive_thought():
|
49 |
+
user_prompt = "Generate a short positive and motivational message."
|
50 |
try:
|
51 |
start_time = time.time()
|
52 |
+
|
53 |
completion = api_client.chat.completions.create(
|
54 |
model=API_MODEL_NAME,
|
55 |
messages=[
|
56 |
+
{
|
57 |
+
"role": "system",
|
58 |
+
"content": (
|
59 |
+
"You are a friendly assistant that ONLY returns a short, positive, "
|
60 |
+
"motivational message. You may think internally, but always wrap your "
|
61 |
+
"internal reasoning in <think>...</think> tags. The user only sees the message "
|
62 |
+
"outside these tags."
|
63 |
+
)
|
64 |
+
},
|
65 |
+
{"role": "user", "content": user_prompt}
|
66 |
]
|
67 |
)
|
68 |
end_time = time.time()
|
69 |
response_time = round(end_time - start_time, 2)
|
70 |
+
|
71 |
+
full_text = completion.choices[0].message["content"].strip()
|
72 |
+
|
73 |
+
# Extract only text after </think>
|
74 |
+
if "</think>" in full_text:
|
75 |
+
final_message = full_text.split("</think>")[-1].strip()
|
76 |
+
else:
|
77 |
+
final_message = full_text
|
78 |
+
|
79 |
+
# Optional: remove surrounding quotes
|
80 |
+
if final_message.startswith('"') and final_message.endswith('"'):
|
81 |
+
final_message = final_message[1:-1].strip()
|
82 |
+
|
83 |
+
return f"{final_message}\n\n(Generated via {API_MODEL_NAME} in {response_time}s)"
|
84 |
+
|
85 |
except Exception as e:
|
86 |
traceback.print_exc()
|
87 |
return f"❌ API Call Failed:\n{type(e).__name__}: {str(e)}"
|
|
|
126 |
|
127 |
# Launch the app
|
128 |
if __name__ == "__main__":
|
129 |
+
demo.launch(share=False)
|